@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.

  1. main「産まれ出でよ、student。お前は俺が見守っている」
  2. main「textbookを作った。俺はこいつの使い手だ」
  3. main「student、一緒にtextbook使おうぜ」
  4. student「まじっすか、あざーす。俺はtextbookのことtbって呼ばせてもらいますね」
  5. main「studentなんかしゃべれや」
  6. student『tb(製造番号:0x10010c9f0)の使い手は一人いるらしいっす』
  7. main「textbook、キミはもう要らない子」
  8. textbook『私(製造番号:0x10010c9f0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』
  9. main「studentなんかしゃべれや」
  10. student『tb(製造番号:0x10010c9f0)の使い手は一人いるらしいっす』(ホントは居ないはず??)
  11. main「student、キミはもう要らない子」
  12. 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.

  1. main「産まれ出でよ、student。お前は俺が見守っている」
  2. main「textbookを作った。俺はこいつの使い手だ」
  3. main「student、一緒にtextbook使おうぜ」
  4. student「まじっすか、あざーす。俺はtextbookのことtbって呼ばせてもらいますね。てことで、俺もtbの使い手のひとりですね」
  5. main「studentなんかしゃべれや」
  6. student『tb(製造番号:0x10010c9f0)の使い手は二人いるらしいっす』
  7. main「textbook、キミはもう要らない子」
  8. textbook「私(製造番号:0x10010c9f0)のことをまだまだ使ってくれる人が一人居るようなので、私はなんとか生きていきます」
  9. main「studentなんかしゃべれや」
  10. student『tb(製造番号:0x10010c9f0)の使い手は一人いるらしいっす』
  11. main「student、キミはもう要らない子」
  12. student『まじっすか?俺(学籍番号:0x1001090c0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます。こうなると、俺はもうtbの使い手ではない。さようなら』
  13. 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.

  1. main「産まれ出でよ、student。お前は俺が見守っている」
  2. main「textbookを作った。俺はこいつの使い手だ」
  3. main「student、一緒にtextbook使おうぜ」
  4. student「まじっすか、あざーす。てか、これコピらせてもらいますね!んでコピったやつのことをtbって呼ばせてもらいます。これで俺はtbの使い手っす」
  5. main「studentなんかしゃべれや」
  6. student『tb(製造番号:0x10010ca60)の使い手は一人いるらしいっす』
  7. main「textbook、キミはもう要らない子」
  8. textbook『私(製造番号:0x10010ca20)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』
  9. main「studentなんかしゃべれや」
  10. student『tb(製造番号:0x10010ca60)の使い手は一人いるらしいっす』
  11. main「student、キミはもう要らない子」
  12. student『まじっすか?俺(学籍番号:0x1001090c0)のことをただ一人見ていたのあなただったんですね、そうですか、消えます。こうなると、俺はもうtbの使い手ではない。さようなら』
  13. textbook『私(製造番号:0x10010ca60)のことをただ一人見ていたのあなただったんですね、そうですか、消えます、さようなら』