Ra1*_*den 0 iphone objective-c nsdictionary ios
我试图创建一个字典(不确定它是否应该NSDictionary或NSMutableDictionary)从NSString一个数组(不确定它是否应该NSArray或NSMutableArray).
属性:
@property(nonatomic, readonly) NSMutableDictionary * categories;
Run Code Online (Sandbox Code Playgroud)
执行:
@synthesize categories = _categories;
- (NSMutableDictionary *)categories{
if(! _categories) {
for(PFObject * each in self.products) {
NSString * currentcategory = [each valueForKey:@"subtitle"];
NSArray * currentlist = [_categories objectForKey:currentcategory];
if(! currentlist) {
currentlist = [[NSArray alloc] init];
}
NSMutableArray * newArray = [currentlist mutableCopy];
[newArray addObject:each];
NSArray * newlist = [NSArray arrayWithArray:newArray];
[_categories setObject:newlist forKey:currentcategory];
}
}
NSLog(@"After constructor the value of the dictionary is %d", [_categories count]);
return _categories;
}
Run Code Online (Sandbox Code Playgroud)
从调试NSLog我发现构造后字典是空的.这里有什么问题,我该如何改变呢?
代码行之后
if(! _categories) {
Run Code Online (Sandbox Code Playgroud)
加
_categories = [NSMutableDictionary new];
Run Code Online (Sandbox Code Playgroud)