ios - 为可变字典中的键赋值

标签 ios nsmutabledictionary

我有一个适用于 iOS 应用的函数。 NSString valuekey 包含值和键,但是它似乎没有分配给 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/

相关文章:

当应用程序在前台时,IOS 在收到推送通知时播放声音

ios - NSMutableDictionary setobject : forkey: crashes with unrecognised selector message

ios - 如何添加到 NSMutableDictionary 键 NSIndexPath 和值 BOOL?

swift - NSMutableDictionary 路径 "unexpectedly found nil while unwrapping an Optional value"

ios - 无法插入 NSMutableDictionary

ios - 如何区分单击手势和双击手势?

ios - 使用 Xcode 7 编译时使用 Mapbox legacy 时没有加载 map 图 block

iphone - 将应用程序部分添加到设置中?

ios - 如何在 Xcode 6.1 中获取 iPod Touch 模拟器

objective-c - 如何对 NSMutableDictionary 进行排序