我正在将学生的 id 存储到 NSMUtableArray 中,我正在尝试扩展该数组以接受两个字段而不仅仅是学生 id
[[NSUserDefaults standardUserDefaults] synchronize]; NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]]; if (archivedArray == nil) { data = [[NSMutableArray alloc] init]; } else { data = [[NSMutableArray alloc] initWithArray:archivedArray]; }
正如您所看到的,数据变量只需要一个字段
最佳答案
如果我正确理解你的问题,你希望数组中的每个“记录”有两个(或更多,稍后)字段。
一种常见的方法是创建一个包含所需字段的字典,然后将字典添加到数组中:
NSDictionary *student1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"key1", @"id",
@"Joe", @"name",
nil];
NSMutableArray *array = [NSMutableArray arrayWithObjects:student1, nil];
您可以随意向学生记录添加更多字段,并向数组添加更多学生。字典的 id 字段也许也不应该是字符串,但这只是一个开始。
关于objective-c - 如何在 NSMutableArray 中存储两个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7130030/