2)NSNull
NSNull大概是Cocoa里最簡單的類了,只有一個方法
+ (NSNull *) null;
可以這樣添加到集合中
[contact setObject: [NSNull null]
forKey: @"home fax machine"];
訪問時:
id homefax;
homefax = [contact objectForKey: @"home fax machine"];
if (homefax == [NSNull null]) {
// ... no fax machine. rats.
}
//[NSNull null]總是返回一樣份數值,所以你可以使用“==”講該值與其他值進行比較……
22、NSDictionary和NSMutableDictionary
A) NSDictionary
字典是關鍵字和其定義的集合,也被成為散列表或關聯數組,使用的是鍵查詢的優化存儲方式
1)創建方法: 使用dictionaryWithObjectsAndKeys:來創建字典
+ (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];
2)常用方法
- (id) objectForKey: (id) aKey;
使用方法:
Tire *tire = [tires objectForKey: @"back- right"]; //如果沒有則會返回nil值
B) NSMutableDictionary
1)創建方法:
可以向類NSMutableDictionary發送dictionary消息
也可以使用函數+ (id) dictionaryWithCapacity: (unsigned int) numItems;
2)常用方法
可以使用setObject:forKey:方法給字典添加元素:
- (void) setObject: (id) anObject forKey: (id) aKey;
- (void) removeObjectForKey: (id) aKey;
使用方法:
NSMutableDictionary *tires;
tires = [NSMutableDictionary dictionary];
[tires setObject: t1 forKey: @"front- left"];
[tires setObject: t2 forKey: @"front- right"];
[tires setObject: t3 forKey: @"back- left"];
[tires setObject: t4 forKey: @"back- right"];
//若已經存在,則會用新值替換原有的值
[tires removeObjectForKey: @"back- left"];
23、不要創建NSString、NSArray或NSDictionary的子類,因為在Cocoa中,許多類實際上是以類簇的方式實現的,即他們是一群隱藏在通用接口之下的與實現相關的類
24、Foundation實例 //查找文件
A)使用枚舉遍歷
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init]; //自動釋放池
NSFileManager *manager; //Cocoa中有很多類都是單實例構架,即只需要一個實例,你真的只需要一個文件管理器
manager = [NSFileManager defaultManager]; // defaultManager方法創建一個屬於我們的NSFileManager對象
NSString *home;
home = [@"~" stringByExpandingTildeInPath]; // stringByExpandingTildeInPath方法可將~替換成當前用戶的主目錄
NSDirectoryEnumerator *direnum; //NSEnumerator的子類
direnum = [manager enumeratorAtPath: home]; //創建一個枚舉條件
NSMutableArray *files;
files = [NSMutableArray arrayWithCapacity: 42]; //把搜索的結果作為文件存儲
NSString *filename;
while (filename = [direnum nextObject]) {//調用nextObject時,都會返回該目錄中的一個文件的另一個路徑,也可搜索子目錄
if ([[filename pathExtension] // pathExtension輸出文件的擴展名(去掉了前面的點.)
isEqualTo: @"jpg"]) {
[files addObject: filename];
}
}
NSEnumerator *fileenum;
fileenum = [files objectEnumerator];
while (filename = [fileenum nextObject]) {
NSLog (@"%@", filename);
}
[pool drain];
return (0);
} // main
B)使用快速遍歷
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *manager;
manager = [NSFileManager defaultManager];
NSString *home;
home = [@"~" stringByExpandingTildeInPath];
NSMutableArray *files;
files = [NSMutableArray arrayWithCapacity: 42];
for (NSString *filename
in [manager enumeratorAtPath: home]) {
if ([[filename pathExtension]
isEqualTo: @"jpg"]) {
[files addObject: filename];
}
}
for (NSString *filename in files) {
NSLog (@"%@", filename);
}