objective-c - 如何在 NSMutableArray 中存储两个字段?

标签 objective-c cocoa nsmutablearray

我正在将学生的 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/

相关文章:

objective-c - 大纲 View :objectValueForTableColumn:byItem: not called

ios - 从 UIDocumentInteractionController 选择 App 会抛出 "LaunchServices: invalidationHandler called"错误

swift - 如何通过终端或 Swift 禁用三指点击手势?

cocoa - NSString stringWithFormat : with random number of replacements

swift - 检测最大化/最小化窗口事件并以编程方式执行

ios - 设置数据库中索引值的数组

ios - 在 NSMutableArray 中搜索对象并删除冗余值的最有效方法

objective-c - NSClassFromString 产生的类不与头文件中定义的类相关联

ios - 用 XML 字符串填充数组

ios - Xcode iOS 通用应用程序图像大小