Objective c 创建包含多个NSMutableDictionary对象的NSMutableArray
我环顾四周,看到了一些关于对包含NSDictionaries的NSMutableArray进行排序的帖子,但没有找到一些具体的示例,说明如何实际制作由NSDictionaries组成的NSMutableArray 我使用NSUserDefaults跟踪用户对几个不同项目的偏好。NSMutableArray将用于跟踪所有项目,NSDictionary用于每个项目的个人首选项 我编写这些方法是为了跟踪preferences类中的项,但我不太确定如何初始化NMutableArray以包含NSDictionaryObjective c 创建包含多个NSMutableDictionary对象的NSMutableArray,objective-c,ios,nsmutablearray,nsdictionary,nsuserdefaults,Objective C,Ios,Nsmutablearray,Nsdictionary,Nsuserdefaults,我环顾四周,看到了一些关于对包含NSDictionaries的NSMutableArray进行排序的帖子,但没有找到一些具体的示例,说明如何实际制作由NSDictionaries组成的NSMutableArray 我使用NSUserDefaults跟踪用户对几个不同项目的偏好。NSMutableArray将用于跟踪所有项目,NSDictionary用于每个项目的个人首选项 我编写这些方法是为了跟踪preferences类中的项,但我不太确定如何初始化NMutableArray以包含NSDicti
-(NSMutableArray *)deviceListArray
{
return [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"device_list_array"];
}
-(void) setDeviceListArray:(NSMutableArray *)deviceListArray
{
[[NSUserDefaults standardUserDefaults] setObject:deviceListArray forKey:@"device_list_array"];
}
如果有人能给我指出正确的方向,我会非常感激。非常感谢你!或者,如果有人有更好的策略来跟踪项目,每个项目都有不同的偏好,那也太棒了
谢谢 一旦您将的对象交给UserDefault,它就拥有该对象-因此您无法更改其中字典的属性 您可以做的是两次复制,其中有一个本地可变数组,其中包含可变字典。您将创建一个大小相同的新可变数组,然后为每个可变字典调用[NSDictionary dictionary WithDictionary:],将新的非可变字典添加到新数组中 如果字典中有可变对象,那么也需要这样做 综上所述,如果你有这么多的东西,我怀疑你没有正确地考虑如何使用默认系统。我几乎总是只保存字符串、数字、颜色等 编辑:代码
NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
for(int i=0; i<10; ++i) {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
// add stuff to the dictionary
[array addObject:dict];
}
// later on want to change second dictionary:
NSMutableDictionary *dict = [array objectAtIndex:1];
// fool around with dict
// no need to re-save it in array, since its mutable
NSMutableArray*array=[NSMutableArray阵列容量:10];
对于(inti=0;i,一旦您将的对象交给userDefaults,它就拥有该对象-因此您无法更改其中字典的属性
您可以做的是两次复制,其中有一个本地可变数组,其中包含可变字典。您可以创建一个大小相同的新可变数组,然后为每个可变字典调用[NSDictionary dictionary WithDictionary:],将新的不可变字典添加到新数组中
如果字典中有可变对象,那么也需要这样做
尽管如此,如果你有这么多东西,我怀疑你没有正确考虑如何使用默认系统。我几乎总是保存字符串、数字、颜色等
编辑:代码
NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
for(int i=0; i<10; ++i) {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
// add stuff to the dictionary
[array addObject:dict];
}
// later on want to change second dictionary:
NSMutableDictionary *dict = [array objectAtIndex:1];
// fool around with dict
// no need to re-save it in array, since its mutable
NSMutableArray*array=[NSMutableArray阵列容量:10];
对于(int i=0;i不确定这是否是您正在寻找的:
#define kDeviceKey1 @"DeviceKey1"
#define kDeviceKey2 @"DeviceKey2"
#define kDeviceKey3 @"DeviceKey3"
#define kDeviceKey4 @"DeviceKey4"
#define kDeviceID @"DeviceID"
NSMutableArray *devices = [[NSMutableArray alloc]init];
NSMutableDictionary *deviceInfo = [[NSMutableDictionary alloc]init];
// create a dictionary record for earch device which contains thing...
// probably employ some kind of loop
deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1], kDeviceID,
// @"DeviceName1", kDeviceID, // or this
@"whateverthing1forDevice1", kDeviceKey1,
@"whateverthing2forDevice1", kDeviceKey2,
@"whateverthing3forDevice1", kDeviceKey3,
@"whateverthing4forDevice1", kDeviceKey4,
nil];
[devices addObject:deviceInfo];
// this is just an example, you would have some kind of loop to replace "whateverthing1forDevice1" with actual data
deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], kDeviceID,
// @"DeviceName2", kDeviceID, // or this
@"whateverthing1forDevice2", kDeviceKey1,
@"whateverthing2forDevice2", kDeviceKey2,
@"whateverthing3forDevice2", kDeviceKey3,
@"whateverthing4forDevice2", kDeviceKey4,
nil];
[devices addObject:deviceInfo];
NSLog(@"devices: %@", devices);
不确定这是否是您想要的:
#define kDeviceKey1 @"DeviceKey1"
#define kDeviceKey2 @"DeviceKey2"
#define kDeviceKey3 @"DeviceKey3"
#define kDeviceKey4 @"DeviceKey4"
#define kDeviceID @"DeviceID"
NSMutableArray *devices = [[NSMutableArray alloc]init];
NSMutableDictionary *deviceInfo = [[NSMutableDictionary alloc]init];
// create a dictionary record for earch device which contains thing...
// probably employ some kind of loop
deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1], kDeviceID,
// @"DeviceName1", kDeviceID, // or this
@"whateverthing1forDevice1", kDeviceKey1,
@"whateverthing2forDevice1", kDeviceKey2,
@"whateverthing3forDevice1", kDeviceKey3,
@"whateverthing4forDevice1", kDeviceKey4,
nil];
[devices addObject:deviceInfo];
// this is just an example, you would have some kind of loop to replace "whateverthing1forDevice1" with actual data
deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], kDeviceID,
// @"DeviceName2", kDeviceID, // or this
@"whateverthing1forDevice2", kDeviceKey1,
@"whateverthing2forDevice2", kDeviceKey2,
@"whateverthing3forDevice2", kDeviceKey3,
@"whateverthing4forDevice2", kDeviceKey4,
nil];
[devices addObject:deviceInfo];
NSLog(@"devices: %@", devices);
嘿,谢谢David。实际上我没有那么多的首选项。它与显示无关,只与外部设备的交互有关,我只需要为每个设备保存大约4件东西。你能详细介绍一下如何分配一个包含可变字典的可变数组吗?这才是真正需要考虑的部分我很生气。我只是没有找到一个好的例子。嘿,谢谢大卫。实际上我没有那么多的偏好。它与显示无关,只是与外部设备的交互,我只需要为每个设备保存大约4件东西。你能详细说明分配具有可变措辞的可变数组的正确方法吗里面是白羊座吗?这真的让我很困惑。我只是没能找到一个好的例子。嘿,非常感谢你的例子。你能不能也给我一个抓取“whateverthing2forDevice2”的语法当它在设备内部时。我必须抓取字典然后从那里抓取它吗?或者我可以一次抓取它吗?我现在把它作为两个步骤,所以这不是什么大问题。我只是想知道它是否可能。谢谢大家举个例子。你也可以给我抓取“whateverthing2forDevice2”的语法吗当它在设备内部时。我必须抓取字典,然后从那里抓取它,还是可以一次完成?我现在只需要两步,所以这不是什么大问题。我只是想知道这是否可能