NSSet和NSArray的对比
/*
NSSet和NSArray的对比
1> 共同点
* 都是集合,都能存放多个OC对象
* 只能存放OC对象,不能存放非OC对象类型(基本数据类型:int、char、float等,结构体,枚举)
* 本身都不可变,都有一个可变的子类
2> 不同点
* NSArray有顺序,NSSet没有顺序
*/
重点内容
#import <Foundation/Foundation.h>
#import "Person.h"
static void ___log(NSSet* set1);
static void ___log(NSSet* set1)
{
NSMutableString * str = [NSMutableString stringWithFormat:@"NSSet{"];
for (id item in set1) {
[str appendFormat:@"%@,",item];
}
//删除最后一个逗号
NSRange rng ={str.length-1,1};
[str deleteCharactersInRange:rng];
[str appendString:@"}"];
NSLog(@"%@",str);
}
演练set判断重复的依
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString* name;
@property NSNumber* age;
@property (nonatomic, strong) NSString * week;
+ (instancetype) personWithName:(NSString*) name withAge:(NSNumber*)age;
@end
#import "Person.h"
@implementation Person
@synthesize name,age;
+ (instancetype)personWithName:(NSString *)name withAge:(NSNumber *)age
{
Person* person=[[Person alloc]init];
person.name=name;
person.age=age;
return person;
}
/**
* 重写hash方法
*/
- (NSUInteger) hash
{
NSLog(@"访问了hash");
NSDate* date=[NSDate new];
return date.timeIntervalSinceReferenceDate;
}
/**
* 重写isEqual方法
*/
- (BOOL)isEqual:(id)object
{
return YES;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//演练set判断重复的依据
Person* jobs=[Person personWithName:@"steven jobs" withAge:[NSNumber numberWithInt:56]];
Person* cook=[Person personWithName:@"david cook" withAge:[NSNumber numberWithInt:54]];
Person* cook2=[Person personWithName:@"david cook" withAge:[NSNumber numberWithInt:54]];
NSSet* personSet =[NSSet setWithObjects:jobs,cook,cook2, nil];
NSLog(@"personSet_count:%lu",personSet.count);
集合的创建
NSLog(@"*************************** 集合的创建 **************************");
NSSet *s1 = [NSSet setWithObjects:@"tom",@"rose", @"jack",@"lily",nil];
NSString *str = [s1 anyObject];// 随机拿出一个元素
NSLog(@"随机取出的元素为:%@", str);
//集合里只能存放OC对象,不能存放非OC对象类型
NSSet* set=[NSSet setWithObjects:@"rose",@"jack",[NSNumber numberWithInt:4],nil];
NSLog(@"set的长度为:%lu",[set count]);
for (id item in set) {
NSLog(@"item:%@",item);
}
集合的遍历
NSLog(@"*************************** 集合的遍历 **************************");
NSArray* arr=[NSArray arrayWithObjects:@"中国",@"日本", @"朝鲜",@"韩国",nil];
NSSet* set1=[NSSet setWithArray:arr];
NSLog(@"%[email protected]",set1.className);
NSArray* arr2=[set1 allObjects];
for (NSString* item in arr2) {
NSLog(@"%@",item);
}
判断交集
NSLog(@"**************************** 判断交集 ***************************");
NSSet* intersect=[NSSet setWithObjects:@"中国",@"日本", nil];
if ([set1 intersectsSet:intersect]) {
NSLog(@"intersect是set1的交集");
}
可变集合操作
NSLog(@"*************************** 可变集合操作 *************************");
//从现有的set转换为MutableSet,获得一份可变集合的拷贝
NSMutableSet* mset=[intersect mutableCopy];
NSLog(@"mset:%@",mset.className);
//添加对象
[mset addObject:@"新加坡"];
NSLog(@"添加对象后的集合为:%@",mset);
//删除对象
[mset removeObject:@"日本"];
NSLog(@"删除对象后的集合为:%@",mset);
//求并集
[mset unionSet:set1];
___log(mset);
NSLog(@"并集为:%@",mset);
//求补集
[mset minusSet:intersect];
___log(mset);
NSLog(@"补集为:%@",mset);
//求交集
[mset intersectsSet:set1];
___log(mset);
NSLog(@"交集为:%@",mset);
NSCountedSet
NSLog(@"*************************** NSCountedSet **************************");
//计算字符串的重复次数 + (instancetype)orderedSet
//The NSCountedSet class declares the programmatic interface to a mutable,
//unordered collection of indistinct objects. A counted set is also known as a bag.
NSString* [email protected]"abc abc 123 ab cd cd";
NSArray* strArr=[sourStr componentsSeparatedByString:@" "];
NSCountedSet* csset=[NSCountedSet setWithArray:strArr];
NSUInteger count=[csset countForObject:@"abc"];
NSLog(@"求得的abc的重复数为:%lu",count);
时间: 2024-10-18 05:33:19