美文网首页
iOS KVC字典模型的简单应用

iOS KVC字典模型的简单应用

作者: 一天天的啊哈哈 | 来源:发表于2020-08-03 22:30 被阅读0次
    #import <Foundation/Foundation.h>
    
    @interface Address : NSObject
    
    @end
    
    @interface Address()
    
    @property (nonatomic, copy)NSString* country;
    @property (nonatomic, copy)NSString* province;
    @property (nonatomic, copy)NSString* city;
    @property (nonatomic, copy)NSString* district;
    
    @end
    
    @implementation Address
    
    @end
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            
            //模型转字典
            Address* add = [Address new];
            add.country = @"China";
            add.province = @"Guang Dong";
            add.city = @"Shen Zhen";
            add.district = @"Nan Shan";
            NSArray* arr = @[@"country",@"province",@"city",@"district"];
            NSDictionary* dict = [add dictionaryWithValuesForKeys:arr]; //把对应key所有的属性全部取出来
            NSLog(@"%@",dict);
            
            //字典转模型
            NSDictionary* modifyDict = @{@"country":@"USA",@"province":@"california",@"city":@"Los angle"};
            [add setValuesForKeysWithDictionary:modifyDict];            //用key Value来修改Model的属性
            NSLog(@"country:%@  province:%@ city:%@",add.country,add.province,add.city);
            
            
        }
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS KVC字典模型的简单应用

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