ClassA.h:
#import <Foundation/Foundation.h> @interface ClassA:NSObject { NSString *name; } -(void)setName:(NSString*)s; -(NSString*)name; -(void)dealloc; @end
ClassA.m:
#import "ClassA.h" @implementation ClassA -(void)setName:(NSString *)s { name=s; [name retain]; } -(NSString*)name { return name; } -(void)dealloc { NSLog(@"delloc coling!"); [name release]; [super dealloc]; } @end
main.m:
#import <Foundation/Foundation.h> #import "ClassA.h" int main(int argc,const char *argv[]) { NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init]; NSMutableString *str1=[NSMutableString stringWithString:@"Sam"]; ClassA *classA=[[ClassA alloc]init]; NSLog(@"Str1 count: %x",[str1 retainCount]); [classA setName:str1]; NSLog(@"str1 count:%x",[str1 retainCount]); [str1 release]; NSLog(@"str1 count:%x",[str1 retainCount]); [classA release]; [pool drain]; return 0; }
console log:
2013-09-22 16:35:50.886 demo[1028:303] Str1 count: 1
2013-09-22 16:35:50.890 demo[1028:303] str1 count:2
2013-09-22 16:35:50.891 demo[1028:303] str1 count:1
2013-09-22 16:35:50.892 demo[1028:303] delloc coling!