基本类型
Objective-C中的基本类型和C语言中的基本类型一样.主要有:int,long,float,double,char,void, bool等.
在Foundation中,也为些数据定义了别名,如:NSInteger为long,CGFloat为double,BOOL等.
Objective-C也可以用C语言的构造类型,如数组、结构体、同用体等。
对于基本类型变量,不需要用指针,也不用手动回收,方法执行结束会自动回收。
NSNumber是Objective-c的数字对象。需求考虑内存释放问题。
2 NSLog( @" %i " ,[number intValue]);
3 NSLog( @" %i " ,[number retainCount]);
2010-12-29 16:02:35.040 HelloWorld[4710:a0f] 123
2010-12-29 16:02:35.042 HelloWorld[4710:a0f] 1
NSString是不可变字符串(NSContantString),其变量和其本类型一样不需要手动释放(它的retainCount为-1)。
NSString赋值:
NSString * str2 = [[NSString alloc] initWithString: @" str... " ]; // 不需要手动释放
因为对NSString赋值,会产生成的对象,所在方法中用NSString作临时对象,也要考虑内存开消问题。
NSMutableString是可变字符串,若用 “[[NSMutableString alloc] init...]”方法初始化,需要考虑手动释放。
2 NSMutableString * mstr = [NSMutableString stringWithString:str];
3 str = @" sss " ;
4 NSLog( @" %@ " ,mstr);
5 NSLog( @" %@ " ,str);
输出:
2 sss
注:因为NSMutableString是NSString的子类,实际应用中很可以把NSMutableString变量赋给NSString。所以若用NSString做类的属性,也会用手动释放的方式:
2 @interface TestProperty : NSObject {
3 NSString * name;
4 NSInteger myInt;
5 }
6
7 @property (copy,nonatomic) NSString * name;
8 @property NSInteger myInt;
9
10 @end
2 @implementation TestProperty
3 @synthesize name;
4 @synthesize myInt;
5
6 - ( void ) dealloc{
7 self.name = nil;
8 [super dealloc];
9 }
10
11 @end
例:
2 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
3
4 NSMutableString * str1 = [NSMutableString stringWithString: @" this is str " ];
5 NSMutableString * str2 = [NSMutableString stringWithString:str1];
6 [str2 appendString: @" sss " ];
7 NSLog( @" %@ " ,str1);
8 NSLog( @" %@ " ,str2);
9 [pool drain];
10 return 0 ;
11 }
12
13 // 输出
14 2010 - 12 - 30 11 : 43 : 13.511 HelloWorld[ 2119 :a0f] this is str
15 2010 - 12 - 30 11 : 43 : 13.521 HelloWorld[ 2119 :a0f] this is strsss
16
17 可以看出str2不是指向str1的,而是新的对象!!
NSArray是不可变数组,一般用于保存固定数据。和NSString不同的是,NSArray有retainCount,所以释放问题。
NSMubleArray是变数组,可以直接对其值进行操作。也可考虑释放问题。
NSMubleArray是NSArray的子类。
2 NSArray * arr_ = [arr sortedArrayUsingSelector:@selector(compare:)];
3 NSLog( @" %i " ,[arr retainCount]);
4 for (NSString * name in arr_){
5 NSLog( @" %@ " ,name);
6 }
7
8 // 输出
9 2010 - 12 - 29 13 : 36 : 16.830 HelloWorld[ 3325 :a0f] 1
10 2010 - 12 - 29 13 : 36 : 16.833 HelloWorld[ 3325 :a0f] Januay
11 2010 - 12 - 29 13 : 36 : 16.833 HelloWorld[ 3325 :a0f] Sep
2 [arr sortUsingSelector:@selector(compare:)];
3 NSLog( @" %i " ,[arr retainCount]);
4 for (NSString * name in arr){
5 NSLog( @" %@ " ,name);
6 }
7
8 // 输出
9 2010 - 12 - 29 13 : 41 : 34.925 HelloWorld[ 3415 :a0f] 1
10 2010 - 12 - 29 13 : 41 : 34.928 HelloWorld[ 3415 :a0f] Januay
11 2010 - 12 - 29 13 : 41 : 34.930 HelloWorld[ 3415 :a0f] Sep
NSSet和NSMutableSet分别是不可变集合和可变集合。集合是一组单值的操作。NSSet和NSMutableSet都需要考虑释放问题。
2 for (id * obj in set ){
3 NSLog( @" %@ " ,obj);
4 }
5 NSLog( @" %i " ,[ set count]);
6 NSLog( @" %i " ,[ set retainCount]);
2010-12-29 13:56:08.397 HelloWorld[3709:a0f] 10
2010-12-29 13:56:08.400 HelloWorld[3709:a0f] aa
2010-12-29 13:56:08.401 HelloWorld[3709:a0f] bb
2010-12-29 13:56:08.401 HelloWorld[3709:a0f] 3
2010-12-29 13:56:08.402 HelloWorld[3709:a0f] 1
NSDictionary和NSMutableDictionary
dictionary是由键-对象对组成的数据集合。NSDictionay和NSMutableDicionary都需要考虑内存释放问题。
2 dictionaryWithObjects:[NSArray arrayWithObjects: @" val1 " , @" val2 " ,nil]
3 forKeys:[NSArray arrayWithObjects: @" key2 " , @" key1 " ,nil]];
4
5 for (NSString * key in dict){
6 NSLog( @" %@ " ,[dict objectForKey:key]);
7 }
8 NSLog( @" %i " ,[dict retainCount]);
9 [pool drain];
2010-12-29 15:37:42.745 HelloWorld[4085:a0f] val2
2010-12-29 15:37:42.748 HelloWorld[4085:a0f] val1
2010-12-29 15:37:42.749 HelloWorld[4085:a0f] 1
由上面结果可以看出Dicionary是按Key排序的。