Objective-C:Foundation架構-常用類-NSDictionary

來源:互聯網
上載者:User

標籤:

  與NSString、NSArray一樣,NSDictionary是不可變的,其對應可變類型為NSMutableDictionary。其用法如下:

#import <Foundation/Foundation.h>@interface Student : NSObject@property (nonatomic, retain) NSString *name;+ (id)studentWithName:(NSString *)name;@end#import "Student.h"@implementation Student+ (id)studentWithName:(NSString *)name {    Student *stu = [[Student alloc] init];    stu.name = name;    return [stu autorelease];}- (void)dealloc {    NSLog(@"%@被銷毀了", _name);    // 釋放name    [_name release];    [super dealloc];}@end

 

#import <Foundation/Foundation.h>#import "Student.h"#pragma mark 字典的初始化void dictCreate() {    // NSDictionary是不可變的    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];        // 最常用的初始化方式    dict = [NSDictionary dictionaryWithObjectsAndKeys:            @"v1", @"k1",            @"v2", @"k2",            @"v3", @"k3", nil];        NSArray *objects = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];    NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];    dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];    NSLog(@"%@", dict);}#pragma mark 字典的基本用法void dictUse() {    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:            @"v1", @"k1",            @"v2", @"k2",            @"v3", @"k3", nil];        // count是計算有多少個索引值對(key-value)    NSLog(@"count=%zi", dict.count);        // 由於NSDictionary是不可變的,所以只能取值,而不能修改值    id obj = [dict objectForKey:@"k2"];    NSLog(@"obj=%@", obj);        // 將字典寫入檔案中    NSString *path = @"/Users/apple/Desktop/dict.xml";    [dict writeToFile:path atomically:YES];        // 從檔案中讀取內容    dict = [NSDictionary dictionaryWithContentsOfFile:path];    NSLog(@"dict=%@", dict);}#pragma mark 字典的用法void dictUse2() {    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:                          @"v1", @"k1",                          @"v2", @"k2",                          @"v3", @"k3", nil];    // 返回所有的key    NSArray *keys = [dict allKeys];    //NSLog(@"keys=%@", keys);        NSArray *objects = [dict allValues];    //NSLog(@"objects=%@", objects);        // 根據多個key取出對應的多個value    // 當key找不到對應的value時,用marker參數值代替    objects = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k4", nil] notFoundMarker:@"not-found"];    NSLog(@"objects=%@", objects);}#pragma mark 遍曆字典void dictFor() {    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:                          @"v1", @"k1",                          @"v2", @"k2",                          @"v3", @"k3", nil];    // 遍曆字典的所有key    for (id key in dict) {        id value = [dict objectForKey:key];        NSLog(@"%@=%@", key, value);    }}#pragma mark 遍曆字典2void dictFor2() {    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:                          @"v1", @"k1",                          @"v2", @"k2",                          @"v3", @"k3", nil];    // key迭代器    NSEnumerator *enumer = [dict keyEnumerator];    id key = nil;    while ( key = [enumer nextObject]) {        id value = [dict objectForKey:key];        NSLog(@"%@=%@", key, value);    }        // 對象迭代器    // [dict objectEnumerator];}#pragma mark 遍曆字典3void dictFor3() {    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:                          @"v1", @"k1",                          @"v2", @"k2",                          @"v3", @"k3", nil];    [dict enumerateKeysAndObjectsUsingBlock:     ^(id key, id obj, BOOL *stop) {        NSLog(@"%@=%@", key, obj);    }];}#pragma mark void dictMemory() {    Student *stu1 = [Student studentWithName:@"stu1"];    Student *stu2 = [Student studentWithName:@"stu2"];    Student *stu3 = [Student studentWithName:@"stu3"];        // 一個對象稱為字典的key或者value時,會做一次retain操作,也就是計數器會+1    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:                          stu1, @"k1",                          stu2, @"k2",                          stu3, @"k3", nil];        // 當字典被銷毀時,裡面的所有key和value都會做一次release操作,也就是計數器會-1}

 

Objective-C:Foundation架構-常用類-NSDictionary

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.