NSArray數組類是Objective-C語言中經常使用的也是重要的一個類,除了開發中經常使用到的一些基礎功能,NSArray及其相關類中還封裝了許多更增強大的功能。有機會總結了一下,與須要的朋友們分享。數組
NSArray中屬性與方法:函數
//獲取數組中元素個數 @property (readonly) NSUInteger count; //經過下標獲數組中的元素 - (ObjectType)objectAtIndex:(NSUInteger)index; //初始化方法 - (instancetype)init; //經過C語言風格的數組建立NSArray對象 須要注意,C數組中須要爲Objective對象,cnt參數爲C數組的長度 //若是cnt的值小於C數組的長度,則會對C數據進行截取賦值,若是大於則程序會崩潰 - (instancetype)initWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt; //數組的歸檔方法 - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; //像數組中追加一個元素 這個方法會返回一個新的數組 - (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject; //像數組中追加一組元素 這個方法會返回一個新的數組 - (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType> *)otherArray; //返回一個字符串,將數組中的元素以separator爲分隔符進行組合 /* NSArray * array = @[@1,@2,@3,@4]; 將打印1,2,3,4 NSString * res = [array componentsJoinedByString:@","]; */ - (NSString *)componentsJoinedByString:(NSString *)separator; //判斷數組中是否包含某個元素 - (BOOL)containsObject:(ObjectType)anObject; //數組的打印方法 @property (readonly, copy) NSString *description; - (NSString *)descriptionWithLocale:(nullable id)locale; - (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level; //獲取第一個包含於另外一個數組中的元素 - (nullable ObjectType)firstObjectCommonWithArray:(NSArray<ObjectType> *)otherArray; //將數組中必定範圍的元素讀取到一個C數組中 objects參數須要爲分配好空間的C指針 - (void)getObjects:(ObjectType __unsafe_unretained [])objects range:(NSRange)range; //獲取某個元素在數值中的下標值 - (NSUInteger)indexOfObject:(ObjectType)anObject; //獲取某個範圍內的元素的下標值 - (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range; //獲取與給定元素相同的元素在數組中的最小下標值 - (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject; //在必定範圍內 獲取與給定元素相同的元素在數組中的最小下標值 - (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range; //判斷兩個數組是否相同 - (BOOL)isEqualToArray:(NSArray<ObjectType> *)otherArray; //獲取數組中第一個元素 @property (nullable, nonatomic, readonly) ObjectType firstObject NS_AVAILABLE(10_6, 4_0); //獲取數組中最後一個元素 @property (nullable, nonatomic, readonly) ObjectType lastObject; //獲取數組的枚舉對象 - (NSEnumerator<ObjectType> *)objectEnumerator; //獲取數組的逆向枚舉對象 - (NSEnumerator<ObjectType> *)reverseObjectEnumerator; /* 這個屬性能夠獲取一個已經排序數組的排序規則 在使用 - (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint; 方法時能夠將此排序規則傳入 對於沒有排序過的數組,使用 - (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context; 方法會自動產生一個這樣的排序規則 */ @property (readonly, copy) NSData *sortedArrayHint; //經過C排序函數進行排序 /* 示例: NSInteger sort(id obj1, id obj2, void *context){ NSNumber *str1 =(NSNumber*) obj1; NSNumber *str2 =(NSNumber*) obj2; if ([str1 intValue] < [str2 intValue]) { return NSOrderedDescending; } else if([str1 intValue] == [str2 intValue]) { return NSOrderedSame; } return NSOrderedAscending; } - (void)viewDidLoad { [super viewDidLoad]; NSArray * array = @[@1,@3,@2,@4]; array = [array sortedArrayUsingFunction:sort context:nil]; NSString * res = [array componentsJoinedByString:@","]; NSLog(@"%@",res); } */ - (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context; //經過C排序函數進行數組排序 - (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint; //使用函數選擇器進行數組排序 - (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator; //獲取數組必定範圍的子數組 - (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range; //將數組寫入文件 - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; //將數組寫入指定url路徑 - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; //是數組中的全部元素調用某個方法選擇器 - (void)makeObjectsPerformSelector:(SEL)aSelector; //功能同上 支持傳參 - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument; //獲取一個下標集合所對應的元素 - (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes; //數組的下標方法 子類重寫 - (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); //對數組中的元素進行枚舉遍歷 - (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); //對數組中的元素進行枚舉遍歷 /* typedef NS_OPTIONS(NSUInteger, NSEnumerationOptions) { NSEnumerationConcurrent = (1UL << 0),//正向枚舉 NSEnumerationReverse = (1UL << 1), //逆向枚舉 }; */ - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); //在一個下標集合中枚舉 - (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); //經過遍歷的方式查找符合條件的元素下標 - (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); //一般 能夠設置遍歷方式 - (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); //同上 在必定下標集合中遍歷 - (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); //經過遍歷的方式查找全部符合條件的元素下標 - (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); //同上 - (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); //同上 - (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); //經過block進行數組排序 - (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); //同上 /* typedef NS_OPTIONS(NSUInteger, NSSortOptions) { NSSortConcurrent = (1UL << 0),//同步排序 NSSortStable = (1UL << 4),//穩定排序 }; */ - (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); //二分查找的枚舉參數 typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) { NSBinarySearchingFirstEqual = (1UL << 8), NSBinarySearchingLastEqual = (1UL << 9), NSBinarySearchingInsertionIndex = (1UL << 10), }; //對區域排序的數組進行二分查找 - (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp NS_AVAILABLE(10_6, 4_0); // binary search //建立對象 + (instancetype)array; //經過一個元素建立數組對象 + (instancetype)arrayWithObject:(ObjectType)anObject; //經過C數組建立數組對象 + (instancetype)arrayWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt; //經過一組元素建立數組對象 + (instancetype)arrayWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION; //經過另外一個數組建立數組對象 + (instancetype)arrayWithArray:(NSArray<ObjectType> *)array; //初始化方法 - (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION; - (instancetype)initWithArray:(NSArray<ObjectType> *)array; - (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag; //經過文件建立數組 + (nullable NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path; //經過url建立數組 + (nullable NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url; 同上 - (nullable NSArray<ObjectType> *)initWithContentsOfFile:(NSString *)path; - (nullable NSArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url; //獲取數組全部元素 須要傳入分配了內存的C指針 - (void)getObjects:(ObjectType __unsafe_unretained [])objects;
NSMutableArray中屬性與方法:atom
//向數組中追加一個元素 - (void)addObject:(ObjectType)anObject; //向數組某個位置插入一個元素 - (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index; //刪除數組中最後一個元素 - (void)removeLastObject; //刪除數組中指定位置的元素 - (void)removeObjectAtIndex:(NSUInteger)index; //替換數組中一個位置的元素 - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject; //初始化 - (instancetype)init NS_DESIGNATED_INITIALIZER; - (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER; //經過數組來追加元素 - (void)addObjectsFromArray:(NSArray<ObjectType> *)otherArray; //交換兩個元素 - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; //刪除全部元素 - (void)removeAllObjects; //在必定範圍內刪除元素 - (void)removeObject:(ObjectType)anObject inRange:(NSRange)range; //刪除一個元素 - (void)removeObject:(ObjectType)anObject; //刪除指定範圍內下標最小的某個元素 - (void)removeObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range; //刪除某個元素 下標最小的 - (void)removeObjectIdenticalTo:(ObjectType)anObject; //刪除必定範圍內的全部元素 - (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0); //經過數組刪除元素 - (void)removeObjectsInArray:(NSArray<ObjectType> *)otherArray; //經過範圍刪除元素 - (void)removeObjectsInRange:(NSRange)range; //替換一組元素 - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray range:(NSRange)otherRange; //替換一組元素 - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray; //設置數組元素 - (void)setArray:(NSArray<ObjectType> *)otherArray; //進行數組排序 - (void)sortUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))compare context:(nullable void *)context; //進行數組排序 - (void)sortUsingSelector:(SEL)comparator; //插入一組元素 - (void)insertObjects:(NSArray<ObjectType> *)objects atIndexes:(NSIndexSet *)indexes; //刪除一組元素 - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes; //替換一組元素 - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray<ObjectType> *)objects; //設置某個下標對應的元素 子類覆寫 - (void)setObject:(ObjectType)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); //進行數組排序 - (void)sortUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); //進行數組排序 - (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); //建立數組 numItems爲元素個數 + (instancetype)arrayWithCapacity:(NSUInteger)numItems; //經過文件建立數組 + (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path; //經過url建立數組 + (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url; 同上 - (nullable NSMutableArray<ObjectType> *)initWithContentsOfFile:(NSString *)path; - (nullable NSMutableArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;
專一技術,熱愛生活,交流技術,也作朋友。url
——琿少 QQ羣:203317592spa