1

I have seen all those links which you have mentioned, But I am not fully understand from that, I am looking a programmatic example for my understanding, that why I use the above program for copy and strong which I have understand bit, but i want to know about strong,retain and weak.

#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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
ragu
  • 133
  • 2
  • 10
  • 1
    check this http://stackoverflow.com/questions/9859719/objective-c-declared-property-attributes-nonatomic-copy-strong-weak – kb920 Mar 23 '16 at 12:23
  • 1
    The code you have in your question is several years out of date. If you're developing for anything above iOS5 you should be updating your code style (and use ARC). – Fogmeister Mar 23 '16 at 16:00

0 Answers0