如何使用两个NSArray';在Objective-C中是什么?
我有两个数组,一个保存键值(myKeys),另一个保存NSString对象(mystring)。我想使用两个数组来使用快速枚举填充单个NSDictionary(myDictionary),但不确定如何填充如何使用两个NSArray';在Objective-C中是什么?,objective-c,for-loop,nsdictionary,fast-enumeration,Objective C,For Loop,Nsdictionary,Fast Enumeration,我有两个数组,一个保存键值(myKeys),另一个保存NSString对象(mystring)。我想使用两个数组来使用快速枚举填充单个NSDictionary(myDictionary),但不确定如何填充 for (NSNumber *key in myKeys) { [self.myDictionary setObject @"here is where the value should go from the 'other' array forKey: key]; } 在这里,我
for (NSNumber *key in myKeys) {
[self.myDictionary setObject @"here is where the value should go from the 'other' array forKey: key];
}
在这里,我应该如何考虑NSArray对象?我建议使用常规for循环,只使用index处的对象,或者只制作自己的计数器并执行相同的操作。但是,如果您想保留foreach并且不想制作自己的计数器,您可以这样做:
[self.myDict setObject:[myStrings objectAtIndex:[myKeys indexOfObject:key]]
forKey: key];
我建议使用一个常规的for循环,只使用object at index,或者只制作自己的计数器并执行相同的操作。但是,如果您想保留foreach并且不想制作自己的计数器,您可以这样做:
[self.myDict setObject:[myStrings objectAtIndex:[myKeys indexOfObject:key]]
forKey: key];
检查文档,NSDictionary可以在没有枚举的情况下执行此操作
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:myObjects forKeys:myKeys];
如果您试图向现有的mutableDictionary添加值,它也可以这样做
[mutableDictionary addEntriesFromDictionary:dictionary];
检查文档,NSDictionary可以在没有枚举的情况下执行此操作
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:myObjects forKeys:myKeys];
如果您试图向现有的mutableDictionary添加值,它也可以这样做
[mutableDictionary addEntriesFromDictionary:dictionary];
如果您真的想加速for循环,可以使用NSEnumerationConcurrent选项。此枚举选项确保您正在使用iDevice上的所有可用资源
[myArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSNumber *key, NSUInteger idx, BOOL *stop) {
[self.myDictionary setObject @"here is where the value should go from the 'other' array" forKey: key];
}];
有关并发循环的更多信息,请参阅本文:
如果你真的想加速for循环,你可以选择NSEnumerationConcurrent选项。此枚举选项确保您正在使用iDevice上的所有可用资源
[myArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSNumber *key, NSUInteger idx, BOOL *stop) {
[self.myDictionary setObject @"here is where the value should go from the 'other' array" forKey: key];
}];
有关并发循环的更多信息,请参阅本文:
你反对在控制变量的索引处使用常规for循环和do对象吗?不,我不知道这是否可行。
+(instancetype)dictionary with objects:(NSArray*)objects forkey:(NSArray*)key
你反对在控制变量的索引处使用常规for循环和do对象吗?不,我不反对,我只是想知道这是否可能。+(instancetype)字典包含对象:(NSArray*)对象分叉:(NSArray*)键
,但NSDictionary不是线程本身。因此,同时从多个线程修改self.myDictionary可能会导致问题。但NSDictionary不是线程self。因此,同时从多个线程修改self.myDictionary可能会导致问题。