词典对象 NSDictionary与NSMutableDictionary
?
做過Java語言或者?C語言開發(fā)的朋友應該很清楚關(guān)鍵字map?吧,它可以將數(shù)據(jù)以鍵值對兒的形式儲存起來,取值的時候通過KEY就可以直接拿到對應的值,非常方便,是一種非常常用的數(shù)據(jù)結(jié)構(gòu)。在Objective-C語言中,當然也有這方面的支持,詞典對象就是做這個事情的,不過在同一個詞典對象中可以保存多個不同類型的數(shù)據(jù),不像Java與C只能保存聲明的相同類型的數(shù)據(jù),這一點還是可以解決不少問題的。
詞典的關(guān)鍵字為NSDictionary與NSMutableDictionary。對OC稍有認識的朋友應該從關(guān)鍵字的結(jié)構(gòu)就可以看出這兩個的區(qū)別。很明顯前者為不可變詞典,后者為可變詞典。
1.創(chuàng)建不可變詞典
[NSDictionary dictionaryWithObjectsAndKeys:..] :?使用鍵值對兒直接創(chuàng)建詞典對象,結(jié)尾必需使用nil標志結(jié)束。
[NSDictionary initWithObjectsAndKeys:..] :使用鍵值對兒初始化詞典對象,結(jié)尾必需使用nil標志結(jié)束。
[dictionary count]:?得到詞典的長度單位。
[dictionary keyEnumerator]:?將詞典的所有KEY儲存在NSEnumerator中,NSEnumerator很像Java語言中的迭代器,使用快速枚舉可以遍歷詞典中所有儲存KEY值。
[dictionary??objectEnumerator]:?將詞典的所有value儲存在NSEnumerator中,用法和上面差不多可用來遍歷KEY對應儲存的Value值。
[dictionary objectForKey:key]:?通過傳入KEY對象可以拿到當前KEY對應儲存的值。
?
#import <UIKit/UIKit.h> #import "MyClass.h" int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //添加我們的測試代碼 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"雨松MOMO",@"name",@"15810463139",@"number", nil]; //得到詞典的數(shù)量 int count = [dictionary count]; NSLog(@"詞典的數(shù)量為: %d",count); //得到詞典中所有KEY值 NSEnumerator * enumeratorKey = [dictionary keyEnumerator]; //快速枚舉遍歷所有KEY的值 for (NSObject *object in enumeratorKey) { NSLog(@"遍歷KEY的值: %@",object); } //得到詞典中所有Value值 NSEnumerator * enumeratorValue = [dictionary objectEnumerator]; //快速枚舉遍歷所有Value的值 for (NSObject *object in enumeratorValue) { NSLog(@"遍歷Value的值: %@",object); } //通過KEY找到value NSObject *object = [dictionary objectForKey:@"name"]; if (object != nil) { NSLog(@"通過KEY找到的value是: %@",object); } int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; }?
2.創(chuàng)建可變詞典對象
NSMutableDictionary?是NSDictionary的子類,所以繼承了NSDictionary的方法。
[NSMutableDictionary dictionaryWithCapacity:10] :?創(chuàng)建一個可變詞典初始指定它的長度為10.,動態(tài)的添加數(shù)據(jù)如果超過10這個詞典長度會自動增加,所以不用擔心數(shù)組越界。
[NSMutableDictionary initWithCapacity:10]??:只是初始化一個詞典的長度為10。
[dictionary setObject: @"雨松MOMO" forKey: @"name"] :向可變的詞典動態(tài)的添加數(shù)據(jù),這里的key是name?值是雨松MOMO。如果詞典中存在這個KEY的數(shù)據(jù)則直接替換這個KEY的值。
[dictionary removeAllObjects..] :?刪除掉詞典中的所有數(shù)據(jù)。
[dictionary removeObjectForKey..] :刪除掉詞典中指定KEY的數(shù)據(jù)。
?
#import <UIKit/UIKit.h> #import "MyClass.h" int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //添加我們的測試代碼 //創(chuàng)建詞典對象,初始化長度為10 NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10]; //向詞典中動態(tài)添加數(shù)據(jù) [dictionary setObject:@"雨松MOMO" forKey:@"name"]; [dictionary setObject:@"15810463139" forKey:@"number"]; //通過KEY找到value NSObject *object = [dictionary objectForKey:@"name"]; if (object != nil) { NSLog(@"通過KEY找到的value是: %@",object); } int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; }?
?
快速枚舉
快速枚舉是一種語言特性,讓我們可以高效并且安全的使用簡明的語法來迭代集合的內(nèi)容。
for…語法
快速枚舉的語法定義如下
| for ( Type?newVariable in expression ) { statements } |
或者
| Type?existingItem; |
| for ( existingItem in expression ) { statements } |
在上述兩種情況中,表達式生成了一個遵循?NSFastEnumeration?協(xié)議的對象 (參見?“Adopting Fast Enumeration”). 迭代的變量在每次循環(huán)中為聲明的對象設(shè)置 。當循環(huán)結(jié)束時,迭代的變量被設(shè)為?nil?。要是循環(huán)提前結(jié)束,那么迭代變量被遺棄,指向最后迭代的對象。
使用快速枚舉的好處:
-
枚舉相比其它方式更加高效,例如?NSEnumerator.
-
語法更加簡明
-
枚舉的使用是“安全的”—枚舉器有一個突變守衛(wèi),因此當你在枚舉進行中試圖驅(qū)修改集合時,就會有一個異常被拋出。
因為迭代過程中的對象的改變是禁止的,故此你可以并發(fā)的執(zhí)行多個迭代。
另一方面,這個特性的行為很像一個標準的?for?循環(huán)。你可以使用?break?來終止迭代或者使用?continue?來跳出本次循環(huán)跳到下個元素。
采用(適配)快速枚舉
如果一個類的實例提供了訪問其它對象集合的方法,那么這個類就可以采用?NSFastEnumeration?協(xié)議. 在?Foundation 框架中的集合類—NSArray,?NSDictionary?以及?NSSet—就采用了這個協(xié)議,就像?NSEnumerator. 很顯然,在?NSArray?和?NSSet?中,枚舉是針對它們的內(nèi)容。對于其它的類,響應的文檔應當明確哪個屬性是用來迭代的。例如,NSDictionary?以及 Core Data 類,?NSManagedObjectModel?提供了對快速迭代的支持;?NSDictionary?枚舉它的鍵,?NSManagedObjectModel?枚舉它的實體。
快速枚舉的使用
下面的例子向我們展示了如何?NSArray?和?NSDictionary?對象是如何使用快速枚舉的.
| NSArray *array = [NSArray arrayWithObjects: |
| @"one", @"two", @"three", @"four", nil]; |
| ? |
| for (NSString *element in array) { |
| NSLog(@"element: %@", element); |
| } |
| ? |
| NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: |
| @"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil]; |
| ? |
| NSString *key; |
| for (key in dictionary) { |
| NSLog(@"English: %@, Latin: %@", key, [dictionary objectForKey:key]); |
| } |
你也可以使用?NSEnumerator?對象來做快速枚舉
| NSArray *array = [NSArray arrayWithObjects: |
| @"one", @"two", @"three", @"four", nil]; |
| ? |
| NSEnumerator *enumerator = [array reverseObjectEnumerator]; |
| for (NSString *element in enumerator) { |
| if ([element isEqualToString:@"three"]) { |
| break; |
| } |
| } |
| ? |
| NSString *next = [enumerator nextObject]; |
| // next = "two" |
如果你想要使用下標,那么你定義一個變量,然后在枚舉里自增計數(shù)就好了
| NSArray *array = <#Get an array#>; |
| NSUInteger index = 0; |
| ? |
| for (id element in array) { |
| NSLog(@"Element at index %u is: %@", index, element); |
| index++; |
| } |
英文原文:點擊打開鏈接
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/heyonggang/p/3449685.html
總結(jié)
以上是生活随笔為你收集整理的词典对象 NSDictionary与NSMutableDictionary的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php的autoload机制
- 下一篇: [个人网站搭建]·Django增加评论功