第一部分
1 // 2 // main.m 3 // 04-@property参数 4 // 5 // Created by apple on 13-8-9. 6 // Copyright (c) 2013年 itcast. All rights reserved. 7 // 8 #import9 10 @interface Book : NSObject 11 12 @end 13 #import "Book.h" 14 15 @implementation Book 16 17 @end 18 19 20 #import 21 #import "Book.h" 22 23 @interface Person : NSObject 24 @property int age; 25 26 // retain : 生成的set方法里面,release旧值,retain新值 27 @property (retain) Book *book; 28 @property (retain) NSString *name; 29 30 @end 31 32 #import "Person.h" 33 34 @implementation Person 35 36 //- (void)setBook:(Book *)book 37 //{ 38 // if (_book != book) 39 // { 40 // [_book release]; 41 // 42 // _book = [book retain]; 43 // } 44 //} 45 46 - (void)dealloc 47 { 48 [_book release]; 49 [_name release]; 50 [super dealloc]; 51 } 52 53 @end 54 55 56 57 58 #import 59 #import "Book.h" 60 61 @interface Student : NSObject 62 63 @property (retain) Book *book; 64 65 @property (retain) NSString *name; 66 67 @end 68 69 70 #import "Student.h" 71 72 @implementation Student 73 74 75 - (void)dealloc 76 { 77 [_book release]; 78 [_name release]; 79 80 [super dealloc]; 81 } 82 83 @end 84 85 86 #import 87 #import "Person.h" 88 #import "Book.h" 89 90 int main() 91 { 92 Book *b = [[Book alloc] init]; 93 Person *p = [[Person alloc] init]; 94 95 p.book = b; 96 97 98 NSLog(@"%ld", [b retainCount]); 99 100 101 [p release];102 [b release];103 return 0;104 }
第二部分
1 // 2 // main.m 3 // 05-@property参数 4 // 5 // Created by apple on 13-8-9. 6 // Copyright (c) 2013年 itcast. All rights reserved. 7 // 8 9 #import10 11 /*12 1.set方法内存管理相关的参数13 * retain : release旧值,retain新值(适用于OC对象类型)14 * assign : 直接赋值(默认,适用于非OC对象类型)15 * copy : release旧值,copy新值16 17 2.是否要生成set方法18 * readwrite : 同时生成setter和getter的声明、实现(默认)19 * readonly : 只会生成getter的声明、实现20 21 3.多线程管理22 * nonatomic : 性能高 (一般就用这个)23 * atomic : 性能低(默认)24 25 4.setter和getter方法的名称26 * setter : 决定了set方法的名称,一定要有个冒号 :27 * getter : 决定了get方法的名称(一般用在BOOL类型)28 */29 30 @interface Person : NSObject31 32 33 // 返回BOOL类型的方法名一般以is开头34 @property (getter = isRich) BOOL rich;35 36 //37 @property (nonatomic, assign, readwrite) int weight;38 // setWeight:39 // weight40 41 //42 @property (readwrite, assign) int height;43 44 @property (nonatomic, assign) int age;45 46 @property (retain) NSString *name;47 @end48 49 #import "Person.h"50 51 @implementation Person52 53 @end54 55 56 57 58 59 #import 60 #import "Person.h"61 62 int main()63 {64 Person *p = [[Person alloc] init];65 66 67 p.rich = YES;68 69 BOOL b = p.isRich;70 71 return 0;72 }