gpt4 book ai didi

ios - 如何初始化从 NSString 到 NSArray 的字典

转载 作者:行者123 更新时间:2023-11-28 18:21:15 27 4
gpt4 key购买 nike

我正在尝试创建一个从 NSString 到数组(不确定它应该是 NSArray 还是 NSMutableArray)。

属性:

@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 我意识到字典在构建后是空的。这里有什么问题,我该如何更改?

最佳答案

代码行之后

 if(! _categories) {

添加

_categories = [NSMutableDictionary new];

关于ios - 如何初始化从 NSString 到 NSArray 的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20224098/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com