Foundation Kit
Cocoa由两个不同的框架组成 Foundation Kit和Application Kit
Foundation框架中有很多诸如NSString,NSArray等低级类和数据类型
- #import <Foundation/Foundation.h>
- int main(int argc, const char *argv[]){
- NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
- //insert code here...
- NSLog(@"Hello, World!");
- [pool drain];
- return 0;
- }
通过alloc创建并通过init初始化了一个池,在结尾处排空,这是Cocoa内存管理的预览
一些有用的数据类型
范围 NSRange
- typedef struct _NSRange{
- unsigned int location;
- unsigned int length;
- }NSRange;
表示相关事物的范围,如字符串中的字符范围或数组中的元素范围
创建一个新的NSRange有3种方式
- //1
- NSRange range;
- range.location=17;
- range.length=4;
- //2
- NSRange range={17,4};
- //3
- NSRange range=NSMakeRange(17,4);
第三种方法的好处是可以在任何能够使用函数的地方使用,比如当作参数
- [anObject flarbulateWithRange: NSMakeRange(13,15)];
几何数据类型 NSPoint,NSSize
- typedef struct _NSPoint{
- float x;
- float y;
- }NSPoint;
- typedef struct _NSSize{
- float width;
- float height;
- }NSSize;
比如Cocoa提供了矩形数据类型
- typedef struct _NSRect{
- NSPoint origin;
- NSSize size;
- }NSRect;
同样提供了NSMakePoint(),NSMakeSize(),NSMakeRect()方法
将这些数据类型作为struct而不是对象的好处是性能更高
字符串 NSString
创建字符串
- NSString *height;
- height=[NSString stringWithFormat: @"Your height is %d feet",5];
类方法
我们所创建的大部分方法是实例方法 用前导减号 - 声明
如果方法用于实现常规功能,用前导加好 + 来声明类方法
就如NSString的stringWithFormat方法
- + (id) stringWithFormat: (NSString *) format, ...;
关于大小
- - (unsigned int) length;
使用方式
- unsigned int length=[height length];
该方法可以正确处理国际字符串
比较
- isEqualToString
- - (BOOL) isEqualToString: (NSString *) aString;
使用方式
- NSString *thing1=@"hello 5";
- NSString *thing2;
- thing2=[NSString stringWithFormat: @"hello %d",5];
- if(thing1 isEqualToString: thing2]){
- NSLog(@"They are the same!");
- }
同样的compare方法
- - (NSCompar isonResult) compare: (NSString *) string;
返回一个NSComparisonResult枚举类型
- type enum _NSComparisonResult{
- NSOrderedAscending=-1,
- NSOrderedSame,
- NSOrderedDescending
- }NSComparisonResult;
如果返回NSOrderedAscending 表示左侧小于右侧 其他类似
不区分大小写的比较
- - (NSComparisonResult) compare: (NSString *) string
- options: (unsigned) mask;
options参数是一个掩码
NSCaseInsensitiveSearch 不区分大小写
NSLiteralSearch 完全比较,区分大小写
NSNumericSearch 比较字符个数而不是字符值 比如100应该排在99以后
- if([thing1 compare: thing2
- options: NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame){
- NSLog(@"They match");
- }
判断字符串内是否包含其他字符串
- - (BOOL) hasPrefix: (NSString *) aString;
- - (BOOL) hasSuffix: (NSString *) aString;
分别检查以特定字符串开头和结尾
- - (NSRange) rangeOfString: (NSString *) aString;
返回匹配的位置,如果找不到 则range.start=NSNotFound
可变性
NSString是不可变的
NSMutableString是可变字符串
两者间比较类似Java中的String和StringBuffer
创建NSMutableString的方法
- + (id) stringWithCapacity: (unsigned) capacity;
该容量只是一个建议
- NSMutableString *string;
- string = [NSMutableString stringWithCapacity: 42];
可以使用一些方法操作该string
- - (void) appendString: (NSString *) aString;
- - (void) appendFormat: (NSString *) format, ...;
使用起来非常方便 也很显而易见
- NSMutableString *string;
- string=[NSMutableString stringWithCapacity: 50];
- [string appendString: @"Hello here"];
- [string appendFormat: @"human %d!",39];
- //得到最后结果Hello here human 39!
类似的
删除字符串中的字符
- - (void) deleteCharactersInRange: (NSRange) range;
NSMutableString是NSString的子类,所以可以使用NSString的所有功能
因此同样可以使用stringWithFormat来创建NSMutableString
集合家族 NSArray NSDictionary等
NSArray可以放入任意类型的对象
两个限制:
1 只能存储Objective-C对象,而不能是C基础类型int,float,enum,struct等
2 不能存储零值nil NULL值
可以通过类方法arrayWithObjects创建,以逗号分割对象列表,并最后以nil表示列表结束
- NSArray *array;
- array=[NSArray arrayWithObjects: @"one",@"two",@"three",nil];
获得对象个数
- - (unsigned) count;
取得特定索引处对象
- - (id) objectAtIndex: (unsigned int) index;
例如遍历一个数组
- int i;
- for(i=0;i<[array count];i++){
- NSLog(@"index %d has %@",i,[array objectAtIndex: i]);
- }
将字符串切分成数组
- -componentsSeparatedByString
将数组合并成字符串
- -componentsJoinedByString
可变数组
NSArray是不可变的,类似的NSMutableArray可变
创建新的可变数组
- + (id) arrayWithCapacity: (unsigned) numItems;
在数组末尾添加对象
- - (void) addObject: (id) anObject;
删除特定位置对象
- - (void) removeObjectAtIndex: (unsigned) index;
枚举 NSEnumerator
通过objectEnumerator向数组请求枚举器
- - (NSEnumerator *) objectEnumerator;
这似乎类似于Java的迭代器Iterator
使用
- NSEnumerator *enumerator;
- enumerator=[array objectEnumerator];
可以从后向前浏览集合 reverseObjectEnumerator
请求下一个对象
- - (id) nextObject;
当返回nil时表示结束
快速枚举
- for(NSString *string in array){
- NSLog(@"I found %@",string);
- }
NSDictionary 有些类似于Map(散列表,关联数组)
类似的NSDictionary不可变,可变的NSMutableDictionary
创建字典的方法
- + (id) dictionaryWithObjectsAndKeys: (id) firstObject, ...;
例如
- Tire *t1=[Tire new];
- Tire *t2=[Tire new];
- Tire *t3=[Tire new];
- Tire *t4=[Tire new];
- NSDictionary *tires;
- tires=[NSDictionary dictionaryWithObjectsAndKeys: t1, @"front=left", t2, @"front-right", t3, @"back-left", t4, @"back-right", nil];
使用objectForKey来获取值
- - (id) objectForKey: (id) aKey;
例如查找右后轮胎
- Tire *tire=[tires objectForKey: @"back-right"];
同样的,对于可变的字典
- + (id) dictionaryWithCapacity: (unsigned int) numItems;
为可变字典添加元素
- - (void) setObject: (id) anObject forKey: (id) aKey;
如果当前已有值,则新值会替代原有的值
删除值
- - (void) removeObjectForKey: (id) aKey;
使用但不扩展
不要自己去创建NSString,NSArray,NSDictionary的子类
各种数值
就和Java中对int,float等有Integer,Float等对象封装,Objectvie-C也提供了NSNumber的包装类
- + (NSNumber *) numberWithChar: (char) value;
- + (NSNumber *) numberWithInt: (int) value;
- + (NSNumber *) numberWithFloat: (float) value;
- + (NSNumber *) numberWithBool: (BOOL) value;
类似的还有long,long long等
例如将一个包装后的数据放入数组
- NSNumber *number;
- number=[NSNumber numberWithInt: 42];
- [array addObject: number];
- [dictionary setObject: num forKey: @"Bork"];
从包装类获取值
- - (char) charValue;
- - (int) intValue;
- - (NSString *) stringValue;
等
NSValue
NSNumber是NSValue的子类,NSValue可以包装任意值
- + (NSValue *) valuseWithBytes: (const void *) value
- objCType: (const char *) type;
例如,将NSRect放入NSArray
- NSRect rect=NSMakeRect(1,2,3,4);
- NSValue *value;
- value=[NSValue valueWithBytes: &rect
- objCType: @encode(NSRect)];
- [array addObject: value];
这里使用@encode编译器指令,它可以接受数据类型的名称并转化为合适的字符串
使用getValue取值
- - (void) getValue: (void *) value;
传递的是存储该数值的变量地址
- value=[array objectAtIndex: 0];
- [value getValue: &rect];
Cocoa提供了常用的将struct型数据转换成NSValue的方法
- + (NSValue *) valueWithPoint: (NSPoint) point;
- + (NSValue *) valueWithSize: (NSSize) size;
- + (NSValue *) valueWithRect: (NSRect) rect;
- - (NSPoint) pointValue;
- - (NSSize) sizeValue;
- - (NSRect) rectValue;
例如,在NSArray中存储和检索NSRect
- value=[NSValue valueWithRect: rect];
- [array addObject: value];
- ...
- NSRect anotherRect=[value rectValue];
NSNull
之前提到nil在NSArray和NSDictionary中有特殊的含义,所以不能把nil放入其中,如果要真的表示没有,Objectvie-C提供了NSNull
使用[NSNull null]==来比较是否为空