1. 程式人生 > >Object-c 中的資料型別

Object-c 中的資料型別

導航:

基本型別

ID

物件型別常見的有

物件型別

-NSLog

-NSNumber

-NSString和NSMutableString

-NSArray和NSMutableArray

-NSSet和NSMutableSet

-NSDictionary和NSMutableDictionary

基本型別:

Objective-C中的基本型別和C語言中的基本型別一樣.主要有:int,long,float,double,char,void, bool等.

在Foundation中,也為些資料定義了別名,如:NSInteger為long,CGFloat為double,BOOL等.

Objective-C也可以用C語言的構造型別,如陣列、結構體、同用體等。

對於基本型別變數,不需要用指標,也不用手動回收,方法執行結束會自動回收。

ID:

在object-c中,物件標識被作為一個特殊的資料型別:id。這個資料型別定義為引用物件的指標。實際上是指向物件例項變數的指標。

物件型別常見的有:

NSlog

NSString

NSInteger

NSURL

NSImage

NSNumber

NSLog

格式如下

%@物件

%d,%i整數

%u無符整形

%f浮點/雙字

%x,%X二進位制整數

%zu size_t %p指標

%e浮點/雙字

%g浮點/雙字

%s C字串

%*s Pascal字串

%c 字元

%C unicha

%lld 64位長整數

(long long)%llu無符64位長整數

%Lf 64位雙字

NSNumber

NSNumber是Objective-c的數字物件。需求考慮記憶體釋放問題。

1   NSNumber *number = [NSNumber numberWithInt:123];
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和NSMutableString

NSString是不可變字串(NSContantString),其變數和其本型別一樣不需要手動釋放(它的retainCount為-1)。

NSString賦值:

NSString *str1 = @"str....";  //(不需要手動釋放)
NSString *str2 = [[NSString alloc] initWithString:@"str..."]; //不需要手動釋放

因為對NSString賦值,會產生成的物件,所在方法中用NSString作臨時物件,也要考慮記憶體開消問題。

NSMutableString是可變字串,若用 “[[NSMutableString alloc] init...]”方法初始化,需要考慮手動釋放。

1     NSString *str = @"this is str...";
2     NSMutableString *mstr = [NSMutableString stringWithString:str];
3     str = @"sss";
4     NSLog(@"%@",mstr);
5     NSLog(@"%@",str);

輸出:

1 this is str...
2 sss

注:因為NSMutableString是NSString的子類,實際應用中很可以把NSMutableString變數賦給NSString。所以若用NSString做類的屬性,也會用手動釋放的方式:

 1 //介面檔案
 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
 1 //實現類
 2  @implementation TestProperty
 3 @synthesize name;
 4 @synthesize myInt;
 5  
 6  -(void) dealloc{
 7     self.name = nil;
 8     [super dealloc];
 9 }
10  
11 @end

例:

程式碼

 1 int main (int argc, const char * argv[]) {
 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和NSMutableArray

NSArray是不可變陣列,一般用於儲存固定資料。和NSString不同的是,NSArray有retainCount,所以釋放問題。

NSMubleArray是變陣列,可以直接對其值進行操作。也可考慮釋放問題。

NSMubleArray是NSArray的子類。

 1     NSArray *arr = [NSArray arrayWithObjects:@"Sep",@"Januay",@"",nil];
 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

程式碼

 1     NSMutableArray *arr = [NSMutableArray   arrayWithObjects:@"Sep",@"Januay",@"",nil];
 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分別是不可變集合和可變集合。集合是一組單值的操作。NSSet和NSMutableSet都需要考慮釋放問題。

程式碼

1     NSSet *set = [NSSet setWithObjects:[NSNumber numberWithInt:10],@"bb",@"aa",@"bb",@"aa",nil];
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都需要考慮記憶體釋放問題。

程式碼

1     NSDictionary *dict = [NSDictionary
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排序的。