ios - 二维数组 Objective-C

标签 ios objective-c nsmutablearray

我正在从两个可变数组创建一个数组。 数组 1 有 3 个对象,数组 2 也有 3 个对象 我收到一条崩溃消息,提示“无法识别的选择器已发送到实例”,为什么这不起作用?

初始化两个数组:

self.array1 =[[NSMutableArray alloc]initWithObjects:@"word", @"word2", nil];
self.array2 =[[NSMutableArray alloc]initWithObjects:@"this goes with word1", @"this goes with the second word", nil];

NSMutableArray *objects;
for (int i=0 ; i<[array1 count]; i++){
   for (int j=0 ; j<[array2 count]; j++){
       objects = [[NSMutableArray alloc]init];
       objects=[array1 objectAtIndex:i][[array2 objectAtIndex:j]];
   }
}

最佳答案

如果我对你的理解正确,你需要使用字典来提供你想要的那种查找机制:

self.array1 = @[@"word", @"word2"].mutableCopy;
self.array2 = @[@"this goes with word1", @"this goes with the second word"].mutableCopy;

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:self.array2
                                                       forKeys:self.array1];

NSLog(@"%@", dictionary[@"word"]);
// Prints "this goes with word1".

关于ios - 二维数组 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22877826/

相关文章:

ios - 自定义标注(mapBox iOS sdk)

ios - 避免单例的最佳方法

ios - 在两个依赖框架中链接静态库时重新定义模块

ios - 解析 eBay API 时使用未声明的标识符 'cell'

objective-c - 窗口关闭时从 NSMutableArray 中移除 NSWindowController

objective-c - 比较时的奇怪行为

ios - 我可以在 iOS 8 的通知栏上显示进度条吗

ios - UIScrollView 是否仅适用于嵌入在导航控件中的 View Controller ?

c++ - NSMutableArray 到 std::vector

ios - 错误: value of type 'Optional<NSMutableArray>' has no subscripts Array[0] in Swift iOS