码迷,mamicode.com
首页 > 其他好文 > 详细

OC之集合

时间:2016-05-07 11:28:11      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

几种常见的集合:

NSArray/NSMutableArray
NSSet/NSMutableSet
NSDictionary/NSMutableDictionary

1. 不可变有序数组:
void Array()
{
    // 创建
    // 快速创建的方法只适于创建不可变数组
    NSArray *s = @[@"jack", @"rose", @"jerry"];
    
    //数组对象的访问
    NSLog(@"%@", s[0]);
    
    // 快速枚举
    for (NSString *obj in s){
        NSLog(@"%@", obj);
    }
    
    // 枚举遍历,适可而止
    [s enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 1) {
            *stop = YES;
        }
        NSLog(@"%@", obj);
    }];
    
<pre name="code" class="objc">    // 数组中对象的个数
    NSInteger count = s.count;
    NSLog(@"count= %ld", count);
}

2.不可变有序数组:创建、添加、删除、遍历等
<pre name="code" class="objc">void MutableArray()
{
    // 创建
    NSMutableArray *s = [NSMutableArray arrayWithObjects:@"jack", @"jerry", @"jane", nil];
    
    // 添加对象
    [s addObject:@"1"];
    
    NSArray *array = @[@"2", @"3", @"4"];
    [s addObjectsFromArray:array];
    
    // 删除对象
    [s removeObject:@"jack"];
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
    <span style="font-family: Arial, Helvetica, sans-serif;">// 删除所有对象</span>
    [s removeAllObjects];
    
    // 快速枚举遍历
    for (NSString *obj in s) {
        NSLog(@"%@", obj);
    }
    
    // 枚举遍历:根据条件停止遍历
    [s enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 4) {
            *stop = YES;
        }
        
        NSLog(@"%@", obj);
    }];
 
<pre name="code" class="objc">    // 输出数组中对象个数
    NSLog(@"%ld", s.count);

}

3.无序不可变集合:
void set()
{
    // 创建
    NSSet *s = [NSSet setWithObjects:@"1", @"2", @"3", nil];
    
    // 随机访问
    NSString *str = [s anyObject];
    NSLog(@"str= %@", str);
    
    // 快速枚举,打印出来是无序的
    for (NSString *obj in s) {
        NSLog(@"NSSet= %@", obj);
    }
<span style="font-family: Arial, Helvetica, sans-serif;">    </span>
<pre name="code" class="objc">    // 集合对象个数
    NSLog(@"count= %ld", s.count);
}


4.无序可变集合:
void mutableSet()
{
    // 创建
    NSMutableSet *s = [NSMutableSet setWithObjects:@"1", @"2", @"3", nil];
    
    // 添加单个对象
    [s addObject:@"4"];
    
    // 添加多个对象
    NSArray *array = @[@"5", @"6"];
    [s addObjectsFromArray:array];
    
    // 删除对象
    [s removeObject:@"1"];
    [s removeAllObjects];
    
    // 枚举遍历
    for (NSString *obj in s) {
        NSLog(@"NSMutableSet= %@", obj);
    }
}
5.无序不可变字典:
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">void</span> dictionary()</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: 'PingFang SC'; color: rgb(0, 132, 0);"><span style="line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);">    </span><span style="line-height: normal; font-family: Menlo;">// </span>创建</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>// <span style="line-height: normal; font-family: 'PingFang SC';">键</span> <span style="line-height: normal; font-family: 'PingFang SC';">:</span> <span style="line-height: normal; font-family: 'PingFang SC';">值(以键为索引寻找对应的值)</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">    NSDictionary *d = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">@{</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"61"</span> : <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="line-height: normal; font-family: 'PingFang SC'; color: rgb(209, 47, 27);">林昆</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"63"</span> :  <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="line-height: normal; font-family: 'PingFang SC'; color: rgb(209, 47, 27);">小强</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"57"</span> :  <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="line-height: normal; font-family: 'PingFang SC'; color: rgb(209, 47, 27);">小顺</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>,  <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"59"</span> : <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="line-height: normal; font-family: 'PingFang SC'; color: rgb(209, 47, 27);">小波</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">}</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);"><span style="font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span>// 值的</span><span style="font-family:PingFang SC;">访问</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">id</span> obj = d[<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"61"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">    NSLog(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"61 -> %@"</span>, obj);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><pre name="code" class="objc"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: 'PingFang SC'; color: rgb(0, 132, 0);"><span style="line-height: normal; font-family: Menlo; color: rgb(0, 0, 0);">    </span><span style="line-height: normal; font-family: Menlo;">// </span>输出字典的键值对数</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">    NSLog(<span style="color: rgb(209, 47, 27);">@"</span><span style="line-height: normal; font-family: 'PingFang SC'; color: rgb(209, 47, 27);">键值对数:</span><span style="color: rgb(209, 47, 27);">%ld"</span>, d.count);</p>

}

6.无序可变字典:

void mutableDictionary()

{

    // 创建

    NSMutableDictionary *s = [NSMutableDictionary dictionary];

    

    // 添加键值对

    [s setObject:@"林昆" forKey:@"61"];

    [s setObject:@"小强" forKey:@"63"];

    [s setObject:@"小顺" forKey:@"57"];

    [s setObject:@"小波" forKey:@"59"];

    

    // 删除键值对

    [s removeObjectForKey:@"61"];


// 访问值对象

    id obj = s[@"61"];

    NSLog(@"61 -> %@", obj);


    // 输出键值对数

    NSLog(@"键值对:%ld", s.count);


}


7.数组对象元素是字典对象:

void arrayAndDictionary()

{

    // 允许有相同的value,当不允许有相同的key

    NSDictionary *d = @{@"林昆" : @"123", @"小强" : @"456", @"小顺" : @"789", @"小波" : @"123"};

      

    // 创建不可变数组元素

    // 元素对象是字典集

    NSArray *s = @[@{@"Name" : @"Linkun", @"QQ" : @"1245205002", @"Favourite" : @[@"Weekness Of People", @"The World Of Commond"]},

                   @{@"qq1" : @"123"},

                   @{@"qq2" : @"234"}];

    

    // 取值

    // 键值命名不能有空格

    NSString *s1 = s[0][@"QQ"];

    NSLog(@"取出QQ号码:%@", s1);

    

    NSString *s2 = s[0][@"Favourite"][0];

    NSLog(@"取出喜欢的书:%@", s2);

    

    // 快速枚举遍历

    for (NSString *obj in s) {

        NSLog(@"%@", obj);

    }

 }













OC之集合

标签:

原文地址:http://blog.csdn.net/sziit_jerry/article/details/51332764

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