日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NSDictionary和NSMutableDictionary good

發布時間:2025/3/15 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NSDictionary和NSMutableDictionary good 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

NSDictionary和NSMutableDictionary

2010-12-20 09:14?914人閱讀?評論(0)?收藏?舉報

?

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);

}BerwinZheng2010-2-13 19:35:36

?

分享到:?上一篇:Reapter 添加刪除事件下一篇:NSDictionary和NSMutableDictionary

轉載于:https://www.cnblogs.com/moonvan/archive/2011/11/15/2249612.html

總結

以上是生活随笔為你收集整理的NSDictionary和NSMutableDictionary good的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。