Tag: nsmutabledictionary

NSMutableDictionary:将变异方法发送给不可变对象

下面的代码返回一个exception,当试图removeObjectForKey时,出现以下错误消息“mutating method sent to immutable object” NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictDeviceIp"]; NSString *key = self.currentDeviceNameText.text; NSString *ipAddressTemp = [storedIpDictionary objectForKey:key]; [storedIpDictionary removeObjectForKey:key]; <—-Crashes here storedIpDictionary[key] = ipAddressTemp; 不知道是什么问题,也许是由于从NSUserDefaults检索字典。 但是下面的代码没有任何问题。 NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictDeviceIp"]; [storedIpDictionary removeAllObjects];

为什么NSUserDefaults无法保存NSMutableDictionary?

我试图用NSUserDefaults保存一个NSMutableDictionary 。 我阅读了很多关于这个主题的post…我也发现了一个选项,工作; 然而不幸的是,它只工作了一次,然后开始只保存(空)。 有人有提示吗? 谢谢 代码保存: [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:dictionary] forKey:@"Key"]; [[NSUserDefaults standardUserDefaults] synchronize]; 加载代码: NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init]; NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"]; dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 将对象添加到NSMutableDictionary代码: [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"Key 1"]; [dictionary setObject:[NSNumber numberWithInt:1] forKey:@"Key 2"]; [dictionary setObject:[NSNumber numberWithInt:2] forKey:@"Key 3"]; 代码到NSLog()值: for (NSString * key in [dictionary allKeys]) { […]

如何在枚举过程中删除NSMutableArray或NSMutableDictionary中的元素?

我正在使用基于块的枚举类似于下面的代码: [[[rows objectForKey:self.company.coaTypeCode] objectForKey:statementType] enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id coaItem, NSUInteger idx, BOOL *stop) { // block code here }] 我想在枚举过程中删除一些对象取决于它们的对象值。 我怎么能这样做? 我知道在枚举过程中操作可变数组或字典(NSMutableArray或NSMutableDictionary)通常是不可能的。 什么是实施这个最好的方法? 谢谢!

问题:在iOS中将对象转换为json

我有一个名为“Store”的主要实体类,如: Store.h: – #import <Foundation/Foundation.h> #import "SignIn.h" @interface Store : NSObject @property (nonatomic, retain) NSString *storeId; @property (nonatomic, retain) NSString *storeProfileId; @property (nonatomic, retain) NSString *storeName; @property (nonatomic, retain) NSString *storeRegion; @property (nonatomic, retain) SignIn *signIn; @end Store.m: – #import "Store.h" @implementation Store @synthesize storeId, storeProfileId, storeName, storeRegion, signIn; – (id) initWithCoder: (NSCoder *)coder { […]

将对象添加到NSMutableArray时无法识别的select器

我有一个NSMutableArray属性,我想要添加对象和删除对象的单身类。 出于某种原因,我得到: -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x1edf24c0 当试图添加到它的exception。 以下是单例界面的相关代码: //outbox item is the type of objects to be held in the dictionary @interface OutboxItem : NSObject @property (nonatomic, assign) unsigned long long size; @end @interface GlobalData : NSObject @property (nonatomic, copy) NSMutableDictionary *p_outbox; + (GlobalData*)sharedGlobalData; @end 单身人士的实施: @implementation GlobalData @synthesize p_outbox; static […]

为什么NSMutableDictionary不想写入文件?

– (void)viewDidLoad { [super viewDidLoad]; if ([[NSFileManager defaultManager] fileExistsAtPath:pathString]) { infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pathString]; } else { infoDict = [[NSMutableDictionary alloc]initWithObjects:[NSArray arrayWithObjects:@"BeginFrame",@"EndFrame", nil] forKeys:[NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithBool:YES], nil]]; if ([infoDict writeToFile:pathString atomically:YES]) { NSLog(@"Created"); } else { NSLog(@"Is not created"); NSLog(@"Path %@",pathString); } } 这是我的代码。 我检查是否创build文件,如果没有 – 我创build一个NSMutableDictionary并将其写入文件的path,但writeToFile方法返回NO 。 问题在哪里? 如果我使用NSFileManager创build这个文件,它可以工作,但是当我想写一个字典时不会。

sorting一个NSMutableDictionary

我有一个NSMutableDictionary将NSString映射到NSString (尽pipe值是NSStrings ,它们实际上只是整数)。 例如考虑下面的映射, "dog" –> "4" "cat" –> "3" "turtle" –> "6" 我想结束字典sorting前十位的值按降序排列。 有人可以告诉我这个代码吗? 也许有一个键arrays和另一个值的数组。 不过,我不介意。 我只是想让它高效。 谢谢!