/****************集合(NSSet)和數組(NSArray)有相似之處,都是存儲不同的對象的地址,不過NSArray是有序的集合,NSSet是無序的集合,同時NSSet可以保證數據的唯一性,當插入相同的數據時,不會有任何效果。集合是一種哈希表,運用散列算法,查找集合中的元素比數組速度更快,但是他沒有順序****************/
/**************** Immutable Set
****************/
@interface NSSet :
NSObject <NSCopying,
NSMutableCopying, NSSecureCoding,
NSFastEnumeration>
//1.判斷兩個對象的值是否相逢,用來查詢set裡面是否有一個對象與傳入對象的值相等。
- (id)member:(id)object;
//2.返回集合中所有對象到一個NSEumerator類型的對象
- (NSEnumerator *)objectEnumerator;
//3.初始化
- (instancetype)init
NS_DESIGNATED_INITIALIZER;
//4.使用一組對象初始化新分配的對象
- (instancetype)initWithObjects:(const
id [])objects count:(NSUInteger)cnt
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCoder:(NSCoder *)aDecoder
NS_DESIGNATED_INITIALIZER;
@end
@interface NSSet (NSExtendedSet)
//5.所有對象的數組屬性
@property (readonly,
copy) NSArray *allObjects;
//6.返回集合中的某個對象
- (id)anyObject;
//7.判斷集合中是否有對象
- (BOOL)containsObject:(id)anObject;
//8.將集合轉化成字符串,也可以通過改變description改變字符串
@property (readonly,
copy) NSString *description;
- (NSString *)descriptionWithLocale:(id)locale;
//9.判斷在一個集合中是否可以插入另外一個集合
- (BOOL)intersectsSet:(NSSet *)otherSet;
//10.判斷兩個集合是否相等
- (BOOL)isEqualToSet:(NSSet *)otherSet;
//11.判斷一個集合是否是另外一個集合的子集。
- (BOOL)isSubsetOfSet:(NSSet *)otherSet;
//12.讓集合中的對象執行指定的方法(遍歷)
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
//13.在集合中追加一個對象
返回set
- (NSSet *)setByAddingObject:(id)anObject
NS_AVAILABLE(10_5,
2_0);
//14.在集合中追加另外一個集合
- (NSSet *)setByAddingObjectsFromSet:(NSSet *)other
NS_AVAILABLE(10_5,
2_0);
//15.在一個集合後面追加一個數組,返回一個新的集合set
- (NSSet *)setByAddingObjectsFromArray:(NSArray *)other
NS_AVAILABLE(10_5,
2_0);
//16.遍歷整個集合
執行在block中
- (void)enumerateObjectsUsingBlock:(void (^)(id obj,
BOOL *stop))block
NS_AVAILABLE(10_6,
4_0);
//17.遍歷整個集合
並且有遍歷選項條件
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void
(^)(id obj, BOOL *stop))block
NS_AVAILABLE(10_6,
4_0);
//18.這個方法標示著傳入函數的參數是一個動態對象類型和一個布爾類型,返回一個布爾類型的代碼塊
- (NSSet *)objectsPassingTest:(BOOL (^)(id obj,
BOOL *stop))predicate
NS_AVAILABLE(10_6,
4_0);
//19.同上,增加了列舉計算選項
- (NSSet *)objectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL
(^)(id obj, BOOL *stop))predicate
NS_AVAILABLE(10_6,
4_0);
@end
@interface NSSet (NSSetCreation)
//20.快速創建set
+ (instancetype)set;
//21.創建set並初始化一個對象
+ (instancetype)setWithObject:(id)object;
//22.創建set並初始化一些對象 分配大小
+ (instancetype)setWithObjects:(const
id [])objects count:(NSUInteger)cnt;
//23.創建set並初始化一些對象 以nil結尾
+ (instancetype)setWithObjects:(id)firstObj, ...
NS_REQUIRES_NIL_TERMINATION;
//24.創建set並初始化添加set
+ (instancetype)setWithSet:(NSSet *)set;
//25.創建set並初始化添加一組數組
+ (instancetype)setWithArray:(NSArray *)array;
//26.創建set並初始化添加一組數組
- (instancetype)initWithObjects:(id)firstObj, ...
NS_REQUIRES_NIL_TERMINATION;
//27.創建set並初始化添加set
- (instancetype)initWithSet:(NSSet *)set;
//28.創建set並初始化添加set 增加標識符
- (instancetype)initWithSet:(NSSet *)set copyItems:(BOOL)flag;
//29.創建set並初始化添加一組數組
- (instancetype)initWithArray:(NSArray *)array;
@end
/**************** Mutable Set
****************/
@interface NSMutableSet :
NSSet
//30.可變set添加一個對象
- (void)addObject:(id)object;
//31.移除set對象
- (void)removeObject:(id)object;
- (instancetype)initWithCoder:(NSCoder *)aDecoder
NS_DESIGNATED_INITIALIZER;
//32.初始化set
- (instancetype)init
NS_DESIGNATED_INITIALIZER;
//33.為set初始化設置大小
- (instancetype)initWithCapacity:(NSUInteger)numItems
NS_DESIGNATED_INITIALIZER;
@end
@interface NSMutableSet (NSExtendedMutableSet)
//34.為set添加對象通過數組
- (void)addObjectsFromArray:(NSArray *)array;
//35.為set插入新的set
- (void)intersectSet:(NSSet *)otherSet;
//36.從接受者中刪除NSSet的左右成員
- (void)minusSet:(NSSet *)otherSet;
//37.移除set的所有對象元素
- (void)removeAllObjects;
//38.取兩個集合的並集,如果給定集合中的成員不是接收集合的成員,則將這個成員添加到接收集合中。
- (void)unionSet:(NSSet *)otherSet;
//39.將otherSet替換了set並且將兩個集合的交集加到set中
- (void)setSet:(NSSet *)otherSet;
@end
@interface NSMutableSet (NSMutableSetCreation)
//40.新建集合
並且設置初始化的長度
+ (instancetype)setWithCapacity:(NSUInteger)numItems;
@end
/**************** Counted Set
****************/
//41.創建集合
並且初始化長度
- (instancetype)initWithCapacity:(NSUInteger)numItems;
// designated initializer
//42.創建集合 以數組初始化對象
- (instancetype)initWithArray:(NSArray *)array;
//43.創建集合 以其他set初始化集合對象
- (instancetype)initWithSet:(NSSet *)set;
//44.統計重復集合對象的個數
- (NSUInteger)countForObject:(id)object;
//45.返回一個枚舉類型
- (NSEnumerator *)objectEnumerator;
//46.添加一個對象
- (void)addObject:(id)object;
//47.移除一個對象
- (void)removeObject:(id)object;
@end
原文鏈接