Collection 類的實(shí)例用于保存指向其他對(duì)象的指針檩帐。Collection 類包括:
NSArray 唾糯、NSMutableArray
NSSet 柄驻、NSMutableSet
-
NSSet
對(duì)象所包含的“內(nèi)容”是無序的县习。而且在一個(gè)NSSet
對(duì)象中掘而,某個(gè)特定的對(duì)象只能出現(xiàn)一次(唯一性)迫悠。 -
NSSet
對(duì)象的最大用處是檢查某個(gè)對(duì)象是否存在鹏漆,NSSet
完成此類任務(wù)的速度比數(shù)組快得多。
NSSet 與 NSArray 的區(qū)別:
-
NSSet
和數(shù)組一樣创泄,是個(gè)數(shù)據(jù)容器艺玲。 - 數(shù)組是有序的,
NSSet
是無序的鞠抑。 - 數(shù)組可以容納重復(fù)對(duì)象饭聚,
NSSet
不能。 - 數(shù)組是有下標(biāo)的搁拙,
NSSet
沒有下標(biāo)秒梳。
NSSet 的創(chuàng)建
// alloc 創(chuàng)建
NSSet *set1 = [[NSSet alloc] initWithObjects:@“張三”,@“李四”, nil];
// 類方法創(chuàng)建
NSSet *set2 = [NSSet setWithObjects:s@“張三”,@“李四”,nil];
其他集合常用創(chuàng)建方法
// 創(chuàng)建一個(gè)新的集合1箕速,集合2有兩個(gè)對(duì)象(a和b)
NSSet *set1 = [NSSet setWithObjects:@"a", nil];
NSSet *set2 = [set1 setByAddingObject:@"b"];
// 通過已有集合7和集合8酪碘,創(chuàng)建一個(gè)新的集合9
NSSet *set7 = [NSSet setWithObjects:@"a", nil];
NSSet *set8 = [NSSet setWithObjects:@"z", nil];
NSSet *set9 = [set7 setByAddingObjectsFromSet:set8];
// 通過已有的集合10和數(shù)組對(duì)象,創(chuàng)建一個(gè)新的集合11
NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSSet *set10 = [NSSet setWithObjects:@"z", nil];
NSSet *set11 = [set10 setByAddingObjectsFromArray:array];
NSSet 與 NSArray 相互轉(zhuǎn)換
// NSSet -> NSArray
NSArray *array = [set1 allObjects];
// NSArray --> NSSet
// 如果同一個(gè)對(duì)象在數(shù)組中出現(xiàn)多次盐茎,那么它只會(huì)添加一次到給定的集合兴垦。 每個(gè)對(duì)象在被添加到集合中時(shí)只會(huì)收到一次保留消息。
NSSet *set3 = [NSSet setWithArray:array];
返回集合元素的個(gè)數(shù)
NSUInteger count = [set1 count];
從容器中隨機(jī)取出一個(gè)元素
NSString *s = [set1 anyObject];
判斷對(duì)象是否在 NSSet 中已經(jīng)存在
- (BOOL)containsObject:(ObjectType)anObject;
示例:
BOOL isContain = [set1 containsObject:@"李四"];
-
NSSet
對(duì)象中的對(duì)象是無序的字柠,所以不能通過索引來訪問探越,只能向NSSet
對(duì)象查詢某一個(gè)對(duì)象是否存在。 - 當(dāng)
NSSet
收到- (BOOL)containsObject:(ObjectType)anObject;
消息時(shí)募谎,會(huì)在其包含的對(duì)象中查找和 anObject 相等的對(duì)象 (內(nèi)容相同扶关,地址可能不同,也就是說可能 【指針指向的內(nèi)容是相等的】,但是他們兩個(gè)【未必是同一塊地址】,返回 YES)。 - 因此泵督,相同的變量(指針指向同一塊內(nèi)存地址)一定是相等的铜异,而相等的變量不一定是相同的哥倔。
NSMutableSet
NSMutableSet
是可變集合,它繼承自 NSSet
揍庄。
常用方法
// 創(chuàng)建一個(gè)空的集合
NSMutableSet *set1 = [NSMutableSet set];
NSMutableSet *set2 = [NSMutableSet setWithObjects:@"1", @"2", nil];
// 集合2“減去”集合3中的元素
[set2 minusSet:set3];
// 集合2與集合3中元素的交集
[set2 intersectSet:set3]咆蒿;
// 集合2與集合3中的元素的并集;
[set2 unionSet:set3];
// 將空集合1設(shè)置為集合3中的內(nèi)容
[set1 setSet:set3];
// 根據(jù)數(shù)組的內(nèi)容刪除集合中的對(duì)象
[set2 addObjectsFromArray:array];
[set2 removeObject:@"1"];
[set2 removeAllObjects];
NSDictionary
- 字典是由 鍵—值對(duì)組成的數(shù)據(jù)容器, 通過 key (鍵蚂子,通常是字符串)沃测,查找對(duì)應(yīng) value (值,可以是任意類型的對(duì)象)食茎。
- 字典中的 鍵—值對(duì)是無序的蒂破。
- 字典對(duì)象中的鍵都是獨(dú)一無二的,如果想要在現(xiàn)存的鍵后再添加第二個(gè)對(duì)象别渔,第一個(gè)鍵—值對(duì)就會(huì)被替換掉附迷。
- 字典中的key可以是任意類型,一般使用字符串作為一個(gè)元素的 key哎媚。
-
NSDictionary
是不可變字典喇伯。
字典的創(chuàng)建
NSArray *array1 = @[@"zhang3",@"zhangfei"];
NSArray *array2 = @[@"li4",@"liming"];
/*
字典的結(jié)構(gòu)
第一個(gè)元素:key:@"zhang" value:array1
第二個(gè)元素:key:@"li" value:array2
...
*/
// 1.alloc方法創(chuàng)建
NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:
array1,@"zhang",
array2,@"li", nil];
// xcode4.4以后對(duì)字典的創(chuàng)建和訪問,語法上做了優(yōu)化。
NSDictionary *dic11 = @{
@"zhang":array1,
@"li":array2
};
// 2.類方法創(chuàng)建字典
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
array1,@"zhang",
array2,@"li", nil];
返回字典元素個(gè)數(shù)
NSUInteger count = [dic1 count];
通過 key 獲取對(duì)應(yīng)的 value
NSArray *array3 = [dic1 objectForKey:@"zhang"];
// 簡便寫法
NSArray *array3 = dic1[@"zhang"];
取得所有的 keys
NSArray *allkeys = [dic1 allKeys];
取得所有的 Values
NSArray *allvalue = [dic1 allValues]; //同下get方法
NSArray *allvalue2 = dic1.allValues;
遍歷字典
// 1.普通遍歷
NSArray *keys = [dic1 allKeys]; //NSDictionary -> NSArray
for (int i =0;i <keys.count;i++) {
NSString *key = keys[i]; //通過下標(biāo)獲取對(duì)象key
NSArray *names =[dic1 objectForKey:key]; //通過key獲取對(duì)應(yīng)的value
NSLog(@"key=%@,value=%@",key,names);
}
// 2.快速遍歷
for (NSString *key in dic1) {
NSArray *names = [dic1 objectForKey:key];
NSLog(@"key=%@,value=%@",key,names);
}
刪除服務(wù)器返回的 value 為 NULL 的情況
#import <Foundation/Foundation.h>
/**
范疇
*/
@interface NSDictionary (MyCustomize)
/**
填充value為空造成的crash
@return 修改后的NSDictionary
*/
- (NSDictionary *)deleteAllNULLValue;
@end
#import "NSDictionary+MyCustomize.h"
/**
自定義范疇類
*/
@implementation NSDictionary (MyCustomize)
- (NSDictionary *)deleteAllNULLValue {
NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init];
for (NSString *keyStr in self.allKeys) {
if ([[self objectForKey:keyStr] isEqual:[NSNull null]]) {
[mutableDic setObject:@"null" forKey:keyStr];
HQLog(@"changed!");
}
else{
[mutableDic setObject:[self objectForKey:keyStr] forKey:keyStr];
HQLog(@"no null");
}
}
return mutableDic;
}
@end
NSMutableDictionary
NSMutableDictionary
是可變字典拨与,字典中的存儲(chǔ)是可以修改的稻据。NSMutableDictionary
繼承于 NSDictionary
。
創(chuàng)建可變字典
// 1.alloc 方法創(chuàng)建
NSArray *array = [NSArray arrayWithObjects:@"張三",@"李四",@"王五", nil];
NSMutableDictionary *mdic = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:array,@"names", nil];
NSMutableDictionary *mdic1 = [[NSMutableDictionary alloc] init];
[mdic1 setObject:array forKey:@"names"]; // 添加元素
NSMutableDictionary *mdic2 = [[NSMutableDictionary alloc] initWithCapacity:3];
添加元素
// 如果key是重復(fù)的截珍,會(huì)將之前的value覆蓋掉
[mdic setObject:@"value" forKey:@"zhang"];
// 將另外一個(gè)字典所有的元素添加到mdic中
[mdic addEntriesFromDictionary:<#(nonnull NSDictionary *)#>];
刪除元素
// 1>根據(jù)Key刪除元素
[mdic removeObjectForKey:@"names"];
// 2>根據(jù)多個(gè)key刪除多個(gè)元素
[mdic removeObjectForKey:@[@"zhang",@"li"]];
// 3>刪除所有元素
[mdic removeAllObjects];
數(shù)組排序
過濾
- 匹配字符串:正則表達(dá)式
// 正則表達(dá)式:驗(yàn)證碼
- (BOOL) validateYzm:(NSString *)yzm {
NSString *regex = @"^\\d{4}$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [predicate evaluateWithObject:yzm];
}
- 過濾 collection
-
NSMutableArray
有一個(gè)名為- filterUsingPredicate:
的方法攀甚,通過該方法可以很方便地剔除所有不能“滿足”傳入的NSPredicate
對(duì)象的對(duì)象箩朴。 -
NSArray
有一個(gè)名為filteredArrayUsingPredicate:
的方法岗喉,可以創(chuàng)建一個(gè)新的數(shù)組,包含所有能夠滿足傳入的NSPredicate
對(duì)象的對(duì)象炸庞。
// 要收回?fù)碛形锲房們r(jià)值高于75美元的員工的物品
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];
NSArray *toBeReclaimed = [allAssets filteredArrayUsingPredicate:predicate];
NSLog(@"toBeReclaimed:%@",toBeReclaimed);
NSSet
也有一個(gè)方法可以來做過濾钱床,代碼如下:
- (NSSet<ObjectType> *)filteredSetUsingPredicate:(NSPredicate *)predicate;
NSMutableSet
也有一個(gè)這樣的方法,如下:
- (void)filterUsingPredicate:(NSPredicate *)predicate;
NSNull
collection 對(duì)象不能保存 nil
埠居。如果要將“空洞”加入集合對(duì)象中查牌,可以使用 NSNull
類。NSNull
類只有一個(gè)實(shí)例滥壕,代表“空”纸颜。
示例代碼如下:
NSMutableArray *hotel = [[NSMutableArray alloc] init];
// 底樓大廳
[hotel addObject:@"lobby"];
// 二樓泳池
[hotel addObject:@"pool"];
// 三樓未建,為空R镩佟P菜铩唠倦!
[hotel addObject:[NSNull null]];
// 四樓臥室
[hotel addObject:@"bedrooms"];
NSIndexSet
這個(gè)類用于存儲(chǔ)有序的索引到某種數(shù)據(jù)結(jié)構(gòu),比如數(shù)組涮较。例如稠鼻,使用這個(gè)類可以生成一份數(shù)組對(duì)象的索引號(hào)清單,這些對(duì)象滿足指定的查詢條件狂票。
- (NSUInteger)indexOfObjectPassingTest:
示例代碼:
NSArray *array = @[@1, @2, @3, @4, @5];
// 遍歷數(shù)組候齿,取出滿足條件的數(shù)組索引
NSUInteger result = [array indexOfObjectPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([(NSNumber *)obj intValue] % 2 == 0) {
return YES;
}else {
return NO;
}
}];
// 如果找到一個(gè)匹配,則查看并打印此項(xiàng)
if (result != NSNotFound) {
NSLog(@"%@",array[result]);
}
// >輸出:2
結(jié)論:只可以返回一個(gè)索引闺属。
- (NSIndexSet *)indexesOfObjectsPassingTest:
示例代碼:
NSArray *array = @[@1, @2, @3, @4, @5];
// 遍歷數(shù)組慌盯,取出滿足條件數(shù)組的所有索引
NSIndexSet *result = [array indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([(NSNumber *)obj intValue] % 2 == 0) {
return YES;
}else {
return NO;
}
}];
// 遍歷 NSIndexSet
[result enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",array[idx]);
}];
// Output:
// 2017-11-17 10:20:13.694049+0800 Test[39023:9364337] 2
// 2017-11-17 10:20:13.694204+0800 Test[39023:9364337] 4
NSIndexSet 常用方法
// 創(chuàng)建一個(gè)空的索引集合
+ (instancetype)indexSet;
// 給定索引創(chuàng)建索引集合
+ (instancetype)indexSetWithIndex:(NSUInteger)value;
// 給定索引范圍創(chuàng)建索引集合
+ (instancetype)indexSetWithIndexesInRange:(NSRange)range;
- (instancetype)initWithIndexesInRange:(NSRange)range NS_DESIGNATED_INITIALIZER;
// 根據(jù)另一個(gè)索引集合創(chuàng)建索引集合
- (instancetype)initWithIndexSet:(NSIndexSet *)indexSet NS_DESIGNATED_INITIALIZER;
// 給定索引值初始化索引集合
- (instancetype)initWithIndex:(NSUInteger)value;
// 判斷兩個(gè)索引集合是否相等
- (BOOL)isEqualToIndexSet:(NSIndexSet *)indexSet;
// 是否包含指定的索引
- (BOOL)containsIndex:(NSUInteger)value;
// 是否包含索引范圍
- (BOOL)containsIndexesInRange:(NSRange)range;
// 遍歷索引集合方法
- (void)enumerateIndexesUsingBlock:(void (NS_NOESCAPE ^)(NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (void)enumerateIndexesWithOptions:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
- (void)enumerateIndexesInRange:(NSRange)range options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
// ...