[英]NSMutableArray not adding object! (using addObject: nor addObjectsFromArray)
[英]NSMutableArray addObject using value and key
我发现了一些这样的代码:
for (int i = 0 ; i < pinyin_array.count; i++) {
[pinyins_arr addObject:@{k_PINYIN : pinyin_array[i]}];
}
其中pinyins_arr是NSMutableArray
而Kk_PINYIN是NSString
。 我不确定这是否正确。 我认为添加键和值对的对象应该是NSMutableDictionary
。 如果是这样,则相同的键将用于多个值,最终数组是什么样的?
@{}
语法是创建NSDictionary
简写。 第二行可以冗长地重写为:
[pinyins_arr addObject:[NSDictionary dictionaryWithObject:pinyin_array[i]] forKey:k_PINYIN]
您正在将NSDictionary
对象添加到NSMutableArray
,因此您将拥有所有具有相同键的字典数组。 是的,如果这是您想要的。
说正确与否不是黑白的。
正确的做法是,存储一系列这样的字典是合法的。
这是不正确的,因为根本不需要使用字典数组,因为字典键没有变化。 您也可以只存储值的数组,而这正是pinyin_array
。
试试这个逻辑
NSObject *collectionIndexString = indexPath;
NSMutableDictionary *tempDict = [myNSMutableArray[row] mutableCopy];
tempDict[@"collectionIndex"] = collectionIndexString;// Setting Key and Value
[myNSMutableArray replaceObjectAtIndex:row withObject:tempDict];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.