我想知道强者,保留者和弱者之间的区别

时间:2016-03-23 12:21:33

标签: objective-c

我已经看到了你提到过的所有这些链接,但我并不完全理解,我正在寻找一个程序化的例子来理解我的原因,为什么我使用上面的程序进行复制和强大的,我理解了一下,但我想知道强者,保留和弱者。

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property(strong,nonatomic)NSString*name;
@end

@implementation Person
@synthesize name;
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *obj = [[Person alloc]init];
        //Class method not need to create object
        NSMutableString *name1=[[NSMutableString 
alloc]initWithString:@"kkk"];
        // Instance Method need to create object
        [obj setName:name1];
        NSLog(@"%@",[obj name]);
        [name1 appendString:@" is Lazy"];
         NSLog(@"%@",[obj name]);
    }
    return 0;
}
2016-03-23 17:33:28.997 Copy&Strong[7225:303] kkk
2016-03-23 17:33:29.006 Copy&Strong[7225:303] kkk is Lazy

// ********************************************* ***************************

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property(retain,nonatomic)NSString*name;
@end

@implementation Person
@synthesize name;
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *obj = [[Person alloc]init];
        //Class method not need to create object
        NSMutableString *name1=[[NSMutableString 
alloc]initWithString:@"kkk"];
        // Instance Method need to create object
        [obj setName:name1];
        NSLog(@"%@",[obj name]);
        [name1 appendString:@" is Lazy"];
         NSLog(@"%@",[obj name]);
    }
    return 0;
}
2016-03-23 17:33:28.997 Copy&Strong[7225:303] kkk
2016-03-23 17:33:29.006 Copy&Strong[7225:303] kkk is Lazy

//**************************************************************************
#import <Foundation/Foundation.h>

@interface Person : NSObject
@property(weak,nonatomic)NSString*name;
@end

@implementation Person
@synthesize name;
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *obj = [[Person alloc]init];
        //Class method not need to create object
        NSMutableString *name1=[[NSMutableString 
alloc]initWithString:@"kkk"];
        // Instance Method need to create object
        [obj setName:name1];
        NSLog(@"%@",[obj name]);
        [name1 appendString:@" is Lazy"];
         NSLog(@"%@",[obj name]);
    }
    return 0;
}
2016-03-23 17:33:28.997 Copy&Strong[7225:303] kkk
2016-03-23 17:33:29.006 Copy&Strong[7225:303] kkk is Lazy

0 个答案:

没有答案