美文网首页
NSDictionry

NSDictionry

作者: 乐乐的熊 | 来源:发表于2018-12-06 21:23 被阅读11次

    https://blog.csdn.net/itianyi/article/details/8661997

    1.setValue和setObject的区别

    1>使用

    1.2者都是为可变字典赋值

    • (void)setValue:(id)value forKey:(NSString *)key;
    • (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
      2.区别
      setValue不仅仅是对字典使用,对任何对象都可以;
      key对应的只能是字符串;
      当value为空时,会调用removeObject删除;

    setObject:是字典特有的方法
    key不仅仅是字符串,是id类型

    @interface NSMutableDictionary(NSKeyValueCoding)
    
    /* Send -setObject:forKey: to the receiver, unless the value is nil, in which case send -removeObject:forKey:.
    
    */
    
    - (void)setValue:(id)value forKey:(NSString *)key;
    
    @end
    
    @interface NSMutableDictionary :NSDictionary
    
    - (void)removeObjectForKey:(id)aKey;
    
    - (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
    
    @end
    

    2 简写

    NSDictionary *dic = @{@"value1":@"key1",@"value2":@"key2"};

    3 枚举不可放字典中

    相关文章

      网友评论

          本文标题:NSDictionry

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