转自 :
1、在Objective语言中可以声明实例变量(Instance Variables)。
变量有四种访问范围:
私有(private): 在声明它的类中可以访问。保护(protected): 在声明它的类中可以访问,在继承类中也可以访问。公共(public):在任何地方都可以访问。包(package):同一个包中的类可以访问。要通过方法才能给实例变量赋值,创建对象都会调用类的初始化方法(init)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #import <Foundation/Foundation.h> //---- @interface section ---- @interface IntObject : NSObject { @ public int myInt; } @end //---- @implementation section ---- @implementation IntObject - (id)init { self = [super init]; if (self) { myInt=100; } return self; } @end //---- program section ---- int main ( int argc, char * argv[]) { @autoreleasepool { IntObject *myIntObject = [[IntObject alloc]init]; NSLog(@ "myInt=%i" ,myIntObject->myInt); } return 0; } |
2、在Objective语言中可以声明用来存取实例变量的实例方法(Instance Methods)。
方法要通过消息传递(message passing)来调用方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #import <Foundation/Foundation.h> //---- @interface section ---- @interface IntObject : NSObject { int myInt; } -( void )setInt:( int )n; -( int )getInt; @end //---- @implementation section ---- @implementation IntObject -( void )setInt:( int )n { myInt=n; NSLog(@ "set myInt=%i" ,n); } -( int )getInt { return myInt; } @end //---- program section ---- int main ( int argc, char * argv[]) { @autoreleasepool { IntObject *myIntObject = [[IntObject alloc]init]; [myIntObject setInt:100]; NSLog(@ "myInt=%i" ,[myIntObject getInt]); } return 0; } |
3、在Objective语言中可以直接声明属性(propertie)。
声明一个属性时会自动声明响应的实例变量和存取方法。
属性可以通过点运算符来存取,也可以使用消息传递。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #import <Foundation/Foundation.h> //---- @interface section ---- @interface IntObject : NSObject { } //@property int myInt; @property (nonatomic,assign) int myInt; @end //---- @implementation section ---- @implementation IntObject @synthesize myInt; @end //---- program section ---- int main ( int argc, char * argv[]) { @autoreleasepool { IntObject *myIntObject = [[IntObject alloc]init]; // [myIntObject setMyInt:100]; // NSLog(@"myInt=%i",[myIntObject myInt]); myIntObject.myInt=100; NSLog(@ "myInt=%i" ,myIntObject.myInt); } return 0; } |
4、声明属性的时候可以设置一些可选参数。
声明属性时有一些可选的属性如:
设置存取:readwrite/readonly,setter=setterName,getter=getterName设置引用:strong/weak/copy/assign/retain非原子性:nonatomic属性默认是可读写的,属性声明时会同时声明同名的私有实例变量和同名的读取方法,以及一个get前缀的设置方法。
假设a指向一个内存,此时这块内存的引用计数(reference counting)是1。
把a赋值给b,那么b也指向了这块内存,此时这块内存的引用计数是2。当释放a以后,只有b指向了这块内存,此时这块内存的引用计数是1。当再释放b以后,此时这块内存的引用计数是0,这时内存会回收。assign是直接赋值,引用计数不会增加。当数据为NSInteger
、CGRect
等标量类型时,应使用assign。retain则在赋值的时候将引用计数加1。copy则会创建并指向一个块新的内存。如果不设置copy/assign/retain的话,默认是使用assign。 一个属性默认是原子性的,当属性在读写的时候会有一个 lock 和unlock的操作,防止在读写的过程中被其他的线程所改变。
在编写单线程程序的时候可以使用nonatomic,这样可以直接读写属性,提高效率。Xcode 4.2中新增ARC功能,关闭这个功能后才能使用下面的代码。
关闭方法:在build setting中找到Objective-C Automatic Reference Counting,设置为No。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #import <Foundation/Foundation.h> //---- @interface section ---- @interface StrObj : NSObject @property (nonatomic,assign) NSMutableString *assStr; @property (nonatomic,retain) NSMutableString *retStr; @property (nonatomic,copy) NSMutableString *copStr; @end //---- @implementation section ---- @implementation StrObj @synthesize assStr,retStr,copStr; @end //---- program section ---- int main ( int argc, char * argv[]) { @autoreleasepool { NSString *myStr = @ "Hello world!" ; NSMutableString *myMutStr = [[NSMutableString alloc] initWithString:myStr]; StrObj *myStrObj = [[StrObj alloc]init]; NSLog(@ "=====List retain count=====" ); NSLog(@ "myStr: %lu" , [myStr retainCount]); NSLog(@ "myMutStr: %lu" , [myMutStr retainCount]); NSLog(@ "myStrObj: %lu" , [myStrObj retainCount]); NSLog(@ "=====List retain count of myMutStr=====" ); [myMutStr retain]; NSLog(@ "myMutStr: %lu" , [myMutStr retainCount]); [myMutStr release]; NSLog(@ "myMutStr: %lu" , [myMutStr retainCount]); NSLog(@ "=====List retain count of myStrObj=====" ); myStrObj.assStr=myMutStr; NSLog(@ "assStr: %lu" , [myStrObj.assStr retainCount]); NSLog(@ "myMutStr: %lu" , [myMutStr retainCount]); myStrObj.retStr=myMutStr; NSLog(@ "retStr: %lu" , [myStrObj.retStr retainCount]); NSLog(@ "myMutStr: %lu" , [myMutStr retainCount]); myStrObj.copStr=myMutStr; NSLog(@ "copStr: %lu" , [myStrObj.copStr retainCount]); NSLog(@ "myMutStr: %lu" , [myMutStr retainCount]); NSLog(@ "=====Change myMutStr=====" ); [myMutStr setString:@ "New World!" ]; NSLog(@ "assStr: %@" , myStrObj.assStr); NSLog(@ "retStr: %@" , myStrObj.retStr); NSLog(@ "copStr: %@" , myStrObj.copStr); } return 0; } |