嗨,代码朋友,
我创建了一个NSMutableDictionary“ _favDictionary”,其中包含图片数据。我从集合视图中从当前单元格获取密钥,该视图包含一个名为“ key”的整数。
FLAMainCell *cell = (FLAMainCell *) sender.view;
NSUInteger *key = cell.key;
NSString *inStr = [NSString stringWithFormat:@"%lu", (unsigned long)key];
_favDictionary[inStr] = storeImage;
- (UIImage *)getFavouriteImages:(NSInteger)index{
return _favDictionary[index];
}
我是Objective-c的初学者,并且找不到像我的方法“ getFavouriteImages”中那样使用整数值访问字典的可能性。我得到的错误是“ NSMutableDictionary不响应ObjectAtIndexedSubscript”。
有人可以告诉我如何通过整数访问字典吗?
首先,如果仅按整数编制索引,则可以使用NSArray
。
如果要使用NSDictionary
,则不必将整数转换为字符串,NSNumber
而可以使用。
FLAMainCell *cell = (FLAMainCell *) sender.view;
NSUInteger *key = cell.key;
NSNumber *inNumber = @(key);
_favDictionary[inNumber] = storeImage;
- (UIImage *)getFavouriteImages:(NSInteger)index{
return _favDictionary[@(index)];
}
如果要使用字符串,则必须在索引之前将index
其NSString
也转换为:
- (UIImage *)getFavouriteImages:(NSInteger)index{
NSString *stringIndex = [NSString stringWithFormat:@"%@", @(index)];
return _favDictionary[stringIndex];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句