集合-字典 方法練習

來源:互聯網
上載者:User

標籤:des   os   io   檔案   資料   for   ar   cti   

- (void)viewDidLoad {

    [super viewDidLoad];

     //用一個或多個索引值對初始化一個字典對象,以值,鍵,值,鍵,值,鍵,...,nil的順序

    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@1,@"key1",@2,@"key2",@3,@"key3" ,nil];

    NSLog(@"dict:%@",dict);

    //用兩個數組初始化一個字典(一個key數組,一個value數組,大小必須相等,否則崩潰

    NSArray *valueArray = @[@"1",@"2",@"3"];

    NSArray *keyArray = @[@"key1",@"key2",@"key"];

    NSDictionary *dict2 = [[NSDictionary alloc] initWithObjects:valueArray forKeys:keyArray];

    NSLog(@"dict:%@",dict2);

    NSDictionary *dict3 = [[NSDictionary alloc] initWithDictionary:dict];

    NSLog(@"dict:%@",dict3);

    

    //儲存字典對象內容到檔案

    NSString *path = @"/Users/apple/Desktop/dict.plist";

    [dict writeToFile:path atomically:YES];

    //從之前儲存的檔案讀取資料到字典對象

    NSDictionary *dict4 = [NSDictionary dictionaryWithContentsOfFile:path];

    NSLog(@"dict4:%@",dict4);

    //不可變字典一樣可以字面常量初始化,key在前,value在後

    NSDictionary *dict5 = @{@"key":@1,@"key1":@2,@"key3":@3};

    NSLog(@"dict5:%@",dict5);

    //通過指定的key來取對應的的對象,如果沒有這個key,返回nil

    NSNumber *num = [dict objectForKey:@"key"];

    NSLog(@"num:%@",num);

    //擷取字典中索引值對的個數

    NSInteger pairCount = [dict count];

    NSLog(@"pairCount:%d",pairCount);

    NSArray *allkeys = [dict allKeys];

    NSArray *allvalues = [dict allValues];

    NSLog(@"allkeys:%@",allkeys);

    NSLog(@"allvalue:%@",allvalues);

   

    //老辦法遍曆

    NSArray *allkeys2 = [dict2 allKeys];

    NSUInteger count = [allkeys2 count];

    for (int i=0; i<count; i++) {

        id key = [allkeys2 objectAtIndex:i];

        id value = [dict2 objectForKey:key];

        NSLog(@"%@:%@",key,value);

    }

    

    //快速枚舉

    for(id key in dict2){

        id value = [dict objectForKey:key];

        NSLog(@"%@:%@",key,value);

    }

    

    //不可變字典對象

    NSMutableDictionary *mdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@1,@"key",@2,@"key2", nil];

    //刪除一個鍵-值

    [mdict removeObjectForKey:@"key"];

    NSLog(@"mdict:%@",mdict);

    //添加一個鍵-值

    [mdict setObject:@3 forKey:@"key3"];

    NSLog(@"mdict:%@",mdict);

    NSMutableDictionary *mdict2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@1,@"key4",@2,@"key4",@3,@"key5", nil];

     //將一個字典對象裡的鍵-值添加到另一個字典對象裡

    [mdict2 addEntriesFromDictionary:mdict];

    NSLog(@"mdcit2:%@",mdict2);

    [mdict removeAllObjects];

    NSLog(@"mdict:%@",mdict);

    

    NSMutableDictionary *mdict3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@1,@"key3",@2,@"key4",@3,@"key5", nil];

    NSMutableDictionary *mdict4 =[[NSMutableDictionary alloc] initWithObjectsAndKeys:nil];

    [mdict4 setDictionary:mdict3];

    NSLog(@"mdict4:%@",mdict4);

    

    NSArray *array = @[@"key4",@2,@"key4",@3,@"key5"];

    [mdict3 removeObjectsForKeys:array];

    //集合

    NSSet *set = [[NSSet alloc] initWithObjects:@1,@2,@3, nil];

    NSSet *set2 = [[NSSet alloc] initWithObjects:@3,@4,@5, nil];

    //隨機取一個元素

    NSNumber *num2 = [set anyObject];

    NSLog(@"num2:%@",num2);

    //判斷集合是否包含某元素

    BOOL b = [set containsObject:@1];

    NSLog(@"%hhd",b);

    //判斷兩個集合是否有交集

    BOOL a = [set intersectsSet:set2];

     NSLog(@"%hhd",a);

    //判斷兩集合是否相等

    BOOL c = [set isEqualToSet:set];

     NSLog(@"%hhd",c);

    //判斷一個集合是否是兩一個集合的子集

    BOOL d = [set isSubsetOfSet:set2];

     NSLog(@"%hhd",d);

    

    //不可變集合

    NSMutableSet *mset = [[NSMutableSet alloc] initWithObjects:@1,@2,@3, nil];

    NSMutableSet *mset2 = [[NSMutableSet alloc] initWithObjects:@2,@3, nil];

   

   //[mset intersectsSet:mset2];

    //[mset addObject:@4];

    //[mset removeObject:@4];

    //NSArray *array3 = @[@4,@5];

    //[mset addObjectsFromArray:array3];

    //[mset minusSet:mset2];

   // [mset removeAllObjects];

    [mset unionSet:mset2];

    NSLog(@"%@",mset);

    

 

    

 

}

    

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.