@propertyによるassign,retain,copy
@propertyによる、assign,retain,copyの際のそれぞれの振る舞いの違いを確認してみました。
assign
#import <Foundation/NSObject.h> #import <stdio.h> // Textbook class @interface Textbook : NSObject @end @implementation Textbook - (void)dealloc { NSLog(@"Textbook(%p) will be dealloced.", self); [super dealloc]; } @end // Student class @interface Student : NSObject { Textbook *tb; } @property (assign) Textbook *tb; // 4. - (void)say; @end @implementation Student @synthesize tb; - (void)say { NSLog(@"Textbook(%p)\'s ref cnt : %d", tb, [tb retainCount]); } - (void)dealloc { NSLog(@"Student(%p) will be dealloced.", self); [tb release]; [super dealloc]; } @end int main() { Student *student = [[Student alloc] init]; // 1. Textbook *textbook = [[Textbook alloc] init]; // 2. student.tb = textbook; // 3. [student say]; // 5. [textbook release]; // 7. [student say]; // 9. [student release]; // 11. }
[47809:903] Textbook(0x10010c9f0)'s ref cnt : 1 // 6.
[47809:903] Textbook(0x10010c9f0) will be dealloced. // 8.
[47809:903] Textbook(0x10010c9f0)'s ref cnt : 1 // 10.
[47809:903] Student(0x1001090c0) will be dealloced. // 12.
- main「産まれ出でよ、student。お前は俺が見守っている」
- main「textbookを作った。俺はこいつの使い手だ」
- main「student、一緒にtextbook使おうぜ」
- student「まじっすか、あざーす。俺はtextbookのことtbって呼ばせてもらいますね」
- main「studentなんかしゃべれや」
- student『tb(製造番号:0x10010c9f0)の使い手は一人いるらしいっす』
- main「textbook、キミはもう要らない子」
- textbook『私(製造番号:0x10010c9f0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』
- main「studentなんかしゃべれや」
- student『tb(製造番号:0x10010c9f0)の使い手は一人いるらしいっす』(ホントは居ないはず??)
- main「student、キミはもう要らない子」
- student『まじっすか?俺(学籍番号:0x1001090c0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』
retain
#import <Foundation/NSObject.h> #import <stdio.h> // Textbook class @interface Textbook : NSObject @end @implementation Textbook - (void)dealloc { NSLog(@"Textbook(%p) will be dealloced.", self); [super dealloc]; } @end // Student class @interface Student : NSObject { Textbook *tb; } @property (retain) Textbook *tb; // 4. - (void)say; @end @implementation Student @synthesize tb; - (void)say { NSLog(@"Textbook(%p)\'s ref cnt : %d", tb, [tb retainCount]); } - (void)dealloc { NSLog(@"Student(%p) will be dealloced.", self); [tb release]; [super dealloc]; } @end int main() { Student *student = [[Student alloc] init]; // 1. Textbook *textbook = [[Textbook alloc] init]; // 2. student.tb = textbook; // 3. [student say]; // 5. [textbook release]; // 7. [student say]; // 9. [student release]; // 11. }
[47810:903] Textbook(0x10010c9f0)'s ref cnt : 2 // 6.
[47810:903] Textbook(0x10010c9f0)'s ref cnt : 1 // 10.
[47810:903] Student(0x1001090c0) will be dealloced. // 12.
[47810:903] Textbook(0x10010c9f0) will be dealloced. // 13.
- main「産まれ出でよ、student。お前は俺が見守っている」
- main「textbookを作った。俺はこいつの使い手だ」
- main「student、一緒にtextbook使おうぜ」
- student「まじっすか、あざーす。俺はtextbookのことtbって呼ばせてもらいますね。てことで、俺もtbの使い手のひとりですね」
- main「studentなんかしゃべれや」
- student『tb(製造番号:0x10010c9f0)の使い手は二人いるらしいっす』
- main「textbook、キミはもう要らない子」
- textbook「私(製造番号:0x10010c9f0)のことをまだまだ使ってくれる人が一人居るようなので、私はなんとか生きていきます」
- main「studentなんかしゃべれや」
- student『tb(製造番号:0x10010c9f0)の使い手は一人いるらしいっす』
- main「student、キミはもう要らない子」
- student『まじっすか?俺(学籍番号:0x1001090c0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます。こうなると、俺はもうtbの使い手ではない。さようなら』
- textbook『私(製造番号:0x10010c9f0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』
copy
#import <Foundation/NSObject.h> #import <stdio.h> // Textbook class @interface Textbook : NSObject <NSCopying> @end @implementation Textbook - (id)copyWithZone:(NSZone *)zone { Textbook *textbook = [[[self class] allocWithZone:zone] init]; return textbook; } - (void)dealloc { NSLog(@"Textbook(%p) will be dealloced.", self); [super dealloc]; } @end // Student class @interface Student : NSObject { Textbook *tb; } @property (copy) Textbook *tb; // 4. - (void)say; @end @implementation Student @synthesize tb; - (void)say { NSLog(@"Textbook(%p)\'s ref cnt : %d", tb, [tb retainCount]); } - (void)dealloc { NSLog(@"Student(%p) will be dealloced.", self); [tb release]; [super dealloc]; } @end int main() { Student *student = [[Student alloc] init]; // 1. Textbook *textbook = [[Textbook alloc] init]; // 2. student.tb = textbook; // 3. [student say]; // 5. [textbook release]; // 7. [student say]; // 9. [student release]; // 11. }
[47811:903] Textbook(0x10010ca60)'s ref cnt : 1 // 6.
[47811:903] Textbook(0x10010ca20) will be dealloced. // 8.
[47811:903] Textbook(0x10010ca60)'s ref cnt : 1 // 10.
[47811:903] Student(0x100109200) will be dealloced. // 12.
[47811:903] Textbook(0x10010ca60) will be dealloced. // 13.
- main「産まれ出でよ、student。お前は俺が見守っている」
- main「textbookを作った。俺はこいつの使い手だ」
- main「student、一緒にtextbook使おうぜ」
- student「まじっすか、あざーす。てか、これコピらせてもらいますね!んでコピったやつのことをtbって呼ばせてもらいます。これで俺はtbの使い手っす」
- main「studentなんかしゃべれや」
- student『tb(製造番号:0x10010ca60)の使い手は一人いるらしいっす』
- main「textbook、キミはもう要らない子」
- textbook『私(製造番号:0x10010ca20)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』
- main「studentなんかしゃべれや」
- student『tb(製造番号:0x10010ca60)の使い手は一人いるらしいっす』
- main「student、キミはもう要らない子」
- student『まじっすか?俺(学籍番号:0x1001090c0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます。こうなると、俺はもうtbの使い手ではない。さようなら』
- textbook『私(製造番号:0x10010ca60)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』