試圖創建一個字典(不知道是否應該NSDictionary
或NSMutableDictionary
)從NSString
到一個數組(不知道是否應該NSArray
或NSMutableArray
)。我如何初始化從NSString的字典來的NSArray
屬性:
@property(nonatomic, readonly) NSMutableDictionary * categories;
實現:
@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;
}
從調試NSLog
我意識到是字典的施工後空。這裏出了什麼問題,我該如何改變它?
哎呀。謝謝! – Ra1nWarden