天天看點

Cocoa資料類型

在Objective-C中依然可以使用所有C的資料類型,但最好還是用它自身的

NSNumber

建立一個值為10的數字對象:NSNumber *num=[NSNumber numberWithInt:10];

建立不同數值類型方法:

numberWithDouble

numberWithFloat

numberWithInt

numberWithLong

numberWithUnsignedShort

numberWithBool

恢複NSNumber不同類型的數值

doubleValue

floatValue

intValue

longValue

unsignedShort

比較兩個NSNumber執行個體的方法:isEqualToNumber:(NSNumber)num

Array

NSArray

建立數組(建立完後大小就無法修改了)

通過方法arrayWithObjects列舉數組中的對象,但輸入nil 作為末對象

NSArray *theArray;

NSString *name1;

NSString *name2;

name1=@"ABC";

name2=@"BCD";

theArray =[NSArray arrayWithObjects:name1,name2,nil];

執行個體的count 方法傳回數組中項個數:[theArray count];

執行個體的objectAtIndex 方法可依據位置索引取得對應位置的對象:[theArray objectAtIndex:0];(索引都是從0開始)

NSMutableArray

此對象繼承自NSArray,則可使用它的所有方法

建立(數組大小及内容可變)

可通過方法 arrayWithCapacity ,開始可以為空,不用有結束對象

NSMutableArray *theArray =[NSMutableArray arrayWithCapacity:0];

添加内容:[theArray addObject:name1];

移除内容:[theArray removeObjectAtIndex:0];(索引從0開始)

插入内容:[theArray insertObject:name2 atIndex:1];

取代内容:[theArray replaceObjectAtIndex:1 withObject:name3];

Boolean

類型:BOOL

内容:YES/NO

用BOOL建立NSNumber對象:[NSNumber numberWithBool:YES]

Date

NSCalendarDate類提供了對時間的操作

取得目前時間:NSDate *theDate=[NSCalendarDate date];

時間格式轉化為字元串:[theDate description];

格式化為确定格式的字元串:[theDate descriptionWithCalendarFormat:@"%A,%B %d,%Y(%I:%M)" timeZone:nil locale:nil];

%A:星期名稱

%B:月份名稱

%d:日數(以兩位數字顯示日數)

%e:日數(以數字顯示日數)

%I:小時

%m:月份(以數字形式顯示)

%M:分鐘

%S:秒數

%Y:年份