NSDictionary和NSMutableDictionary

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        NSDictionary *dic=[NSDictionary dictionaryWithObject:@"lnj" forKey:@"name"];
//        NSString *name=[dic objectForKey:@"name"];
//        NSLog(@"name=%@",name);
        
//        NSDictionary *dic=[NSDictionary dictionaryWithObjects:@[@"lnj",@"30"] forKeys:@[@"name",@"age"]];
//        NSLog(@"%@,%@",[dic objectForKey:@"name"],[dic objectForKey:@"age"]);
        
//        NSDictionary *dic =@{@"name":@"lnj"};
//        NSLog(@"%@",dic[@"name"]);

//        NSDictionary *dic=@{@"name":@"lnj",@"age":@"30",@"height":@"1.75"};
//        NSLog(@"%@,%@,%@",dic[@"name"],dic[@"age"],dic[@"height"]);
        
        
        //字典的遍历
//        NSLog(@"count=%lu",[dic count]);
//        for (int i=0; i<dic.count; i++) {
//            NSArray *keys=[dic allKeys];
//            NSString *key=keys[i];
//            NSString *value=dic[key];
//            NSLog(@"%@,%@",key,value);
//            
//        }
//        for (NSObject *key in dic) {
////            NSLog(@"%@",key);
//            NSString *value=dic[key];
//            NSLog(@"%@,%@",key,value);
//        }
        
        //字典和数组不同,字典中保存的数据是无序的
//        [dic enumerateKeysAndObjectsUsingBlock:^(id  key,id  value,BOOL *stop) {
//                        NSLog(@"%@,%@",key,value);
//
//        }];
        
        //3.字典文件读写
        NSDictionary *dic=@{@"name":@"lnj",@"age":@"30",@"height":@"1.75"};
        [dic writeToFile:@"/Users/qianlilin/Desktop/info.plist" atomically:YES];
        
        NSDictionary *newDict=[NSDictionary dictionaryWithContentsOfFile:@"/Users/qianlilin/Desktop/info3.plist"];
        NSLog(@"%@",newDict);
//        NSArray *arr=@[@10,@20,@5];
//        [arr writeToFile:@"/Users/qianlilin/Desktop/info1.plist" atomically:YES];
//        NSArray *newArray=[NSArray arrayWithContentsOfFile:@"/Users/qianlilin/Desktop/info2.plist"];
//        NSLog(@"%@",newArray);
    }
    return 0;
}
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        NSMutableArray *arrM=[NSMutableArray arrayWithObject:@"qll"];
        NSMutableArray *arrM=[NSMutableArray array];
        [arrM addObject:@"JJJ"];
        NSLog(@"%@",arrM);
        [arrM addObjectsFromArray:@[@"lmj",@"lnj"]];
        NSLog(@"%@",arrM);
        
//        [arrM addObject:@[@"lmj",@"lnj"]];
//        NSLog(@"%@",arrM);
        
        [arrM insertObject:@"xcp" atIndex:1];
        NSLog(@"%@",arrM);
        
        NSRange range=NSMakeRange(2, 2);
        NSIndexSet *set=[NSIndexSet indexSetWithIndexesInRange:range];
        [arrM insertObjects:@[@"123",@"456"] atIndexes:set];
        NSLog(@"%@",arrM);
        
        
        [arrM removeObject:@"123"];
        NSLog(@"%@",arrM);

        [arrM removeLastObject];
        NSLog(@"%@",arrM);
        
        [arrM removeObjectAtIndex:0];
        NSLog(@"%@",arrM);
        
        [arrM replaceObjectAtIndex:1 withObject:@"M"];
        NSLog(@"%@",arrM);

        NSLog(@"%@",[arrM objectAtIndex:0]);
        
        
        NSLog(@"%@",arrM[0]);
        
        arrM[0]=@"741";
        NSLog(@"%@",arrM[0]);
    }
    return 0;
}

上课所写

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        NSDictionary *dii=@{@"a":@"天安门"};
//        NSDictionary *dii1=@{@"a":@"长城"};
//        NSArray *c=@[dii,dii1];
//        NSDictionary *dic=@{@"city":@"北京市",@"Citys":c};
//        NSArray *arr=@[dic];
//        [arr writeToFile:@"/Users/qianlilin/Desktop/info4.plist" atomically:YES];
        
        
        NSString *path=@"/Users/qianlilin/Desktop/city.plist";
        NSArray  *arr=[NSArray arrayWithContentsOfFile:path];
    /*    for (NSDictionary *a in arr) {
//            NSLog(@"%@",a);
            
            NSArray *CI=[a objectForKey:@"Citys"];
            NSLog(@"%@",[a objectForKey:@"city"]);
            for (NSDictionary *cc in CI) {
//                NSLog(@"%@",cc);
                NSLog(@"%@",[cc objectForKey:@"a"]);
                
            }
        }*/
        
        
        
        for (NSDictionary *china in arr) {
            //        NSLog(@"%@",a);
            NSArray *CI=[china objectForKey:@"Cities"];
            NSLog(@"%@",china[@"State"]);
            // NSLog(@"%@",cc);
            for (NSDictionary *cc in CI) {
                NSLog(@"%@",[cc objectForKey:@"city"]);
            }
        }
        

    }
    return 0;
}

 

你可能感兴趣的:(NSDictionary和NSMutableDictionary)