NSString property: copy or retain?

前端 未结 10 977
予麋鹿
予麋鹿 2020-11-22 02:53

Let\'s say I have a class called SomeClass with a string property name:

@interface SomeClass : NSObject
{
    NSString* name;
}

@p         


        
10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 03:16

    @interface TTItem : NSObject    
    @property (nonatomic, copy) NSString *name;
    @end
    
    {
        TTItem *item = [[TTItem alloc] init];    
        NSString *test1 = [NSString stringWithFormat:@"%d / %@", 1, @"Go go go"];  
        item.name = test1;  
        NSLog(@"-item.name: point = %p, content = %@; test1 = %p", item.name, item.name, test1);  
        test1 = [NSString stringWithFormat:@"%d / %@", 2, @"Back back back"];  
        NSLog(@"+item.name: point = %p, content = %@, test1 = %p", item.name, item.name, test1);
    }
    
    Log:  
        -item.name: point = 0x9a805a0, content = 1 / Go go go; test1 = 0x9a805a0  
        +item.name: point = 0x9a805a0, content = 1 / Go go go, test1 = 0x9a84660
    

提交回复
热议问题