//
// main.m
// OC05-task-03
//
// Created by Xin the Great on 15-1-25.
// Copyright (c) 2015年 Xin the Great. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
///NSSet -- 集合
//不可变
//集合没有重复的元素,而且无序
NSSet *set = [[NSSet alloc] initWithObjects:@"a",@"b",@"a", nil];
NSLog(@"set is %@",set);
//类方法初始化
NSSet *set1 = [NSSet setWithObjects:@"1",@"2",@"x",@"y", nil];
NSLog(@"set1 is %@",set1);
//获取集合元素的个数
NSUInteger count = [set1 count];
NSLog(@"count is %ld", count);
//获取所有的集合元素
NSArray *list = [set1 allObjects];
NSLog(@"list is %@", list);
//获得任意一个元素
id value = [set1 anyObject];
NSLog(@"value is %@", value);
//判断集合中是否包含某一个元素
BOOL isTure = [set1 containsObject:@"u"];
NSLog(@"isTure is %d", isTure);
NSMutableSet -- 可变集合
NSMutableSet *mutableSet = [NSMutableSet set];
//添加元素
[mutableSet addObject:@"x"];
NSLog(@"mutableSet is %@", mutableSet);
//批量添加
NSArray *values = @[@"1", @"2", @"3", @"3", @"3"];
[mutableSet addObjectsFromArray:values];
NSLog(@"mutableSet is %@", mutableSet);
//删除元素
[mutableSet removeObject:@"x"];
NSLog(@"mutableSet is %@", mutableSet);
//删除所有
[mutableSet removeAllObjects];
NSLog(@"mutableSet is %@", mutableSet);
//集合的遍历
NSSet *set3 = [NSSet setWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
//传统遍历,对集合来说必须要转化为数组
NSArray *arr3 = [set3 allObjects];
for (int i = 0; i < set3.count; i++) {
//不可以通过下标取元素
// NSString *str = set3[i];
NSString *str = arr3[i];
NSLog(@"str is %@", str);
}
NSLog(@"------------------------------------------");
//快速遍历
for (NSString *str in set3) {
NSLog(@"str is %@", str);
}
}
return 0;
}
NSSet -- 集合&&NSMutableSet -- 可变集合
最新推荐文章于 2022-04-08 17:11:37 发布