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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java enumerator_NSEnumerator使用

發布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java enumerator_NSEnumerator使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

集合類(如:NSArray、NSSet、NSDictionary等)均可獲取到NSEnumerator, 該類是一個抽象類,沒有用來創建實例的公有接口。NSEnumerator的nextObject方法可以遍歷每個集合元素,結束返回nil,通過與while結合使用可遍歷集合中所有項。

例子中使用了與前例相同的Photo對象,具體定義參考?隱式循環?這一節。

#import

#import "Photo.h"

int main (int argc, const char * argv[])

{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//while+NSEnumerator

//定義對象的數組

NSArray *array = [NSArray arrayWithObjects:[[Photo alloc] init], [[Photo alloc] init],[[Photo alloc] init], nil];

//通過objectEnumberator獲取集合的NSEnumerator

NSEnumerator *myEnumerator = [array objectEnumerator];

Photo *photo;

//nextObject遍歷每一項,結束返回nil

//注意這里使用的是“=”號,所以最外面還要再添加一對()

while((photo = [myEnumerator nextObject]))

{

[photo draw];

}

[pool drain];

return 0;

}

NSSet allObjects

#import

#import "Photo.h"

int main (int argc, const char * argv[])

{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//NSSet allObjects

NSSet *set = [NSSet setWithObjects:[[Photo alloc] init], [[Photo alloc] init],[[Photo alloc] init], nil];

//allObjects僅能獲取NSArray,需要再次調用objectEnumberator

//并且獲取的數組順序不確定。

NSEnumerator *myEnumerator = [[set allObjects] objectEnumerator];

Photo *photo;

while((photo = [myEnumerator nextObject]))

{

[photo draw];

}

[pool drain];

return 0;

}

NSDictionary allValues allKeys

#import

#import "Photo.h"

int main (int argc, const char * argv[])

{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//NSDictionary allValues

//NSDictionary 添加項時是value在前j,key在后的,與C#相反

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:

[[Photo alloc] init], @"p1",

[[Photo alloc] init], @"p2",

[[Photo alloc] init], @"p3", nil];

//allValues 只能獲取字典中值的數組列表,注意列表是無序的。

NSEnumerator *myEnumerator = [[dict allValues] objectEnumerator];

Photo *photo;

while((photo = [myEnumerator nextObject]))

{

[photo draw];

}

//allKeys 遍歷字典中的所有key列表,然后能過objectForKey獲取值。注意列表是無序的。

myEnumerator = [[dict allKeys] objectEnumerator];

NSString *key;

while((key = [myEnumerator nextObject]))

{

//objectForKey從字典中獲取key對應的值

[[dict objectForKey:key] draw];

}

[pool drain];

return 0;

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

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

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