我有一个适用于 iOS 应用的函数。 NSString value
和 key
包含值和键,但是它似乎没有分配给 NSMutableDictionary data
行 [data setValue:value forKey:key]
-(NSMutableDictionary *)parseCode:(NSString *)string{
NSArray *array = [string componentsSeparatedByString:@"||"];
NSMutableDictionary *data;
for(id object in array){
NSArray *objectAndKey = [object componentsSeparatedByString:@"::"];
NSString *key =[objectAndKey objectAtIndex:0];
NSString *value = [objectAndKey objectAtIndex:1];
[data setValue:value forKey:key];
[self alertMeWithString:[data valueForKey:key]];
}
return data;
}
最佳答案
您需要初始化和分配您的 NSMutableDictionary。
NSMutableDictionary *data = [NSMutableDictionary dictionary];
或者您可以明确地使用
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
关于ios - 为可变字典中的键赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772105/