美文网首页iOS 笔记程序员
iOS 给字典中插入nil的几种情景

iOS 给字典中插入nil的几种情景

作者: 道道明明白白 | 来源:发表于2017-12-20 22:52 被阅读79次

    NSString *str = nil;

    NSDictionary *dic = @{@"key":str};

    //zs20171220 会崩溃'*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]

    字面量添加nil 会崩溃

    NSString *str = nil;

    NSDictionary *dicParam = [[NSDictionary alloc] initWithObjectsAndKeys:@"",@"key1",str,@"key2",nil];

    NSLog(@"dicParam____%@",dicParam[@"key2"]);//zs20171220 value 值可以为nil,但是这个时候nil意味着结束添加了

    NSLog(@"dicParam____%@",dicParam.allKeys); NSLog(@"dicParam____%@",dicParam[@"key1"]);//zs20171220 key 不能设置成nil 否则会崩溃

    NSString *str = nil;

    NSMutableDictionary *dicMutable = [NSMutableDictionary dictionaryWithCapacity:0];

    [dicMutable setValue:str forKey:@"key1"];//zs20171220 插入nil 不会崩溃

    [dicMutable setValue:@"ddd" forKey:@"key2"];

    [dicMutable setValue:nil forKey:@"key3"];//zs20171220 插入nil 不会有这个key 和值

    NSLog(@"___%@",dicMutable);

    相关文章

      网友评论

        本文标题:iOS 给字典中插入nil的几种情景

        本文链接:https://www.haomeiwen.com/subject/vdnnwxtx.html