码迷,mamicode.com
首页 > 编程语言 > 详细

OC-Day4____数组与字典

时间:2015-03-03 20:34:04      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

2015.3.3

 

#import <Foundation/Foundation.h>

 

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

    @autoreleasepool {

        //利用i值遍历数组

        NSArray *array = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five", nil];

        //NSLog(@"array = %@", array);

#if 0

        NSInteger len = [array count];

        for (NSInteger i=0; i<len; i++) {

            NSLog(@"%@", [array objectAtIndex:i]);

        }

#endif

#if 0

        //枚举器遍历数组

        //正序枚举数组

        NSEnumerator  *enum1 = nil;

        enum1 = [array objectEnumerator];

        id obj = nil;//obj对象指针必须定义在循环外面

        while (obj = [enum1 nextObject]) {

            NSLog(@"%@", obj);

        }

        //倒序枚举数组

        NSEnumerator *enum2 = [array reverseObjectEnumerator];

        id obj2 = nil;

        while (obj2 = [enum2 nextObject]) {

            NSLog(@"%@", obj2);

        }

#endif

        //快速枚举法遍历数组

        for (id obj in array) {

            NSLog(@"%@", obj);

        }

    

    }

    return 0;

}

 

 

 

 

#import <Foundation/Foundation.h>

 

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

    @autoreleasepool {

        NSArray *array = @[@"one",@"two",@"three",@"four",@"five",@"six",@"seven"];

//        array = [array sortedArrayUsingSelector:@selector(compare:)];

//        NSLog(@"array = %@", array);

        NSMutableArray *mulArray = [NSMutableArray arrayWithArray:array];

        array = [mulArray sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"array = %@", array);

    }

    return 0;

}

 

 

 

 

#import <Foundation/Foundation.h>

//NSDictionary

//创建不可变字典

//字典对象中的没有元素都是一个键值对

//key : vlaue

//key 与 vlaue可以为任意类型的对象

//通常 key 用字符串类的对象表示

 

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

    @autoreleasepool {

        //xcode 4.6之后

        //创建字典对象

        NSDictionary *dict = @{@"one":@"1",@"two":@"2",@"three":@"3",@"four":@"4"};

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

        //- (instancetype)initWithObjectsAndKeys:(id)firstObject...

        NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three", nil];

        NSLog(@"dict1 = %@", dict1);

        

        //用传入字典对象创建一个新的字典对象

        //- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;

        NSDictionary *dict2 = [NSDictionary dictionaryWithDictionary:dict1];

        NSLog(@"dict2 = %@", dict2);

        //用value的数组及key的数组创建字典对象

        //- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

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

        NSArray *keys = @[@"one",@"two",@"three",@"four"];

        NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        NSLog(@"dict3 = %@", dict3);

        

        //字典的其它操作方法

        

        //获取字典对象键值对个数

        NSInteger cnt = [dict count];

        NSLog(@"cnt = %ld", cnt);

        

        //通过key获取对应的值

        id obj = [dict objectForKey:@"three"];

        NSLog(@"obj = %@", obj);

        //获取字典中所有的key

        NSArray *allKeys = [dict allKeys];

        NSLog(@"allKeys = %@", allKeys);

        

        //通过值找到该值对应的所有的key

        NSDictionary *dict4 = @{@"one":@"1", @"two":@"1",@"three":@"3",@"four":@"3"};

        NSArray *keys1 = [dict4 allKeysForObject:@"1"];

        NSLog(@"keys1 = %@", keys1);

        

        //获取所有的值

        NSArray *values1 = [dict4 allValues];

        NSLog(@"values1 = %@", values1);

        //判断两个字典是否相等

        BOOL ret = [dict isEqualToDictionary:dict4];

        NSLog(@"ret = %d", ret);

        

        //字典遍历

        //i值遍历

        NSInteger len = [dict count];

        NSArray *keys2 = [dict allKeys];

        for (NSInteger i=0; i<len; i++) {

            NSLog(@"key = %@ value = %@", [keys2 objectAtIndex:i], [dict objectForKey:[keys2 objectAtIndex:i]]);

        }

        

        //枚举器法

        NSEnumerator *keyEnum = [dict keyEnumerator];

        id keyObj = nil;

        while (keyObj = [keyEnum nextObject]) {

            NSLog(@"%@:%@",keyObj,[dict objectForKey:keyObj]);

        }

        //枚举值

        NSEnumerator *valueEnum = [dict objectEnumerator];

        id valueObj = nil;

        while (valueObj = [valueEnum nextObject]) {

            NSLog(@"%@", valueObj);

        }

        //快速枚举法

        //枚举的是key而不是value, 但是可以通过key查找对应的值

        for (id keyObj in dict) {

            NSLog(@"%@:%@", keyObj,[dict objectForKey:keyObj]);

        }

    }

    return 0;

}

 

 

 

#import <Foundation/Foundation.h>

//NSMutableDictionary

//创建可变字典对象

//继承与NSDictionary

 

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

    @autoreleasepool {

        //创建一个空的字典对象

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];

        NSMutableDictionary *mulDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four", nil];

        NSLog(@"mulDict = %@", mulDict);

        //通过key删除对应的键值对

        [mulDict removeObjectForKey:@"four"];

        NSLog(@"mulDict = %@", mulDict);

        //创建一个指定容量的可变字典对象

        NSMutableDictionary *mulDict1 = [NSMutableDictionary dictionaryWithCapacity:12];

        //把传入的字典键值对添加到可变字典中

        [mulDict1 addEntriesFromDictionary:mulDict  ];

        [mulDict1 addEntriesFromDictionary:@{@"five":@"5",@"six":@"6",@"one":@"2"}];

        NSLog(@"mulDict1 = %@", mulDict1);

        //key在字典中是唯一的

        NSMutableDictionary *mulDict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"one", nil];

        NSLog(@"mulDict2 = %@", mulDict2);

        //删除所有的键值对

        [mulDict2 removeAllObjects];

        NSLog(@"mulDict2 = %@", mulDict2);

        //利用key的数组, 删除key数组对应的键值对

        NSArray *keyArray = @[@"one",@"two"];

        [mulDict1 removeObjectsForKeys:keyArray];

        NSLog(@"mulDict1 = %@", mulDict1);

        //用传入的字典对象重置可变字典

        [mulDict1 setDictionary:@{@"hello":@"1",@"world":@"2"}];

        NSLog(@"mulDict1 = %@", mulDict1);

        

    }

    return 0;

}

 

OC-Day4____数组与字典

标签:

原文地址:http://www.cnblogs.com/-yun/p/OC_day4.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!