Cocoa数据类型

来源:互联网 发布:酷狗音乐无法连接网络 编辑:程序博客网 时间:2024/04/30 09:00

在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:年份