NSMutableDictionary与copy 问题举例【入门

2020-03-12  本文已影响0人  码农二哥

场景1


@interface WPersion : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
@implementation WPersion
-(NSString *)description
{
    return [NSString stringWithFormat:@"name:%@, age:%@", _name, @(_age)];
}
@end
{
        NSMutableDictionary *md = [[NSMutableDictionary alloc] init];
        [md setObject:@"123" forKey:@"str"];
        
        WPersion *persion = [[WPersion alloc] init];
        persion.name = @"name";
        persion.age = 10;
        [md setObject:persion forKey:@"obj"];
    
        NSMutableDictionary *mmd = [[NSMutableDictionary alloc] initWithDictionary:md copyItems:YES];        
    }

场景2


@interface WPersion : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
@implementation WPersion
-(NSString *)description
{
    return [NSString stringWithFormat:@"name:%@, age:%@", _name, @(_age)];
}
@end
@interface ViewController ()
@property (nonatomic, copy) NSMutableDictionary *mdict;
@end
...
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    {
        NSMutableDictionary *md = [[NSMutableDictionary alloc] init];
        [md setObject:@"123" forKey:@"str"];
        
        WPersion *persion = [[WPersion alloc] init];
        persion.name = @"name";
        persion.age = 10;
        [md setObject:persion forKey:@"obj"];        
        self.mdict = md;
    }
    
    [_mdict setObject:@"456" forKey:@"str1"];
}
...
上一篇 下一篇

猜你喜欢

热点阅读