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

NSSet和NSMutableSet

时间:2015-01-01 08:56:29      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

        //NSArray 自然顺序
        //NSSet是无序的
        //注意:这个是最为重要的功能 NSSet中不能够存储重复的数据,可以用它来去除重复的值
        NSString * str1 = @"one";
        NSString * str2 = @"two";
        NSString * str3 = @"three";
        NSSet * set = [[NSSet alloc] initWithObjects:str1,str2,str3,str1, nil];
        NSLog(@"set %@",set);
        
        //count
        NSLog(@"count %ld",set.count);
        
       BOOL isContains =  [set containsObject:str1];
        if (isContains)
        {
            NSLog(@"YES");
        }
        else
        {
            NSLog(@"NO");
        }
        
        //4.遍历
        
        NSEnumerator * enumerator = [set objectEnumerator];
        NSString * value;
        while (value = [enumerator nextObject]) {
            NSLog(@"value %@",value);
        }

 

NSString * str1 = @"one";
        NSString * str2 = @"two";
        
        //1 创建一个可变集合
        NSMutableSet * muSet = [[NSMutableSet alloc] init];
        
        //2.增加值
        [muSet addObject:str1];
        [muSet addObject:str2];
        
        NSLog(@"muSet %@",muSet);
        
        //3.删除
        [muSet removeObject:str1];
        
        NSLog(@"muSet %@",muSet);
        
        //4.删除所有
//        [muSet removeAllObjects];
    
        NSLog(@"muSet %@",muSet);
        
        //5.遍历
        NSEnumerator * en = [muSet objectEnumerator];
        NSString * value;
        while (value = [en nextObject]) {
            NSLog(@"value %@",value);
        }

 

NSSet和NSMutableSet

标签:

原文地址:http://www.cnblogs.com/cwhking/p/4196997.html

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