iOS 深入解析之NSArray
生活随笔
收集整理的這篇文章主要介紹了
iOS 深入解析之NSArray
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>@property (readonly) NSUInteger count;
/*
返回位于指定索引處的對象。如果index超出數組的末尾(也就是說,如果index大于或等于count返回的值),則會引發NSRangeException。
*/
- (ObjectType)objectAtIndex:(NSUInteger)index;
/*
初始化一個空的數組
*/
- (instancetype)init NS_DESIGNATED_INITIALIZER;
/*
描述 :初始化新分配的數組,以包含給定C數組中給定數量的對象。
元素以與它們在對象中出現的順序相同的順序添加到新數組中,直到但不包括索引計數。
以這種方式初始化一個不可變數組之后,就不能修改了。
這個方法是一個指定的初始化器。
參數
objects : C對象的數組。
cnt : 要包含在新數組中的對象C數組中的值的數量。 這個數字將是新數組的數量 - 它不能是負數或大于對象中元素的數量。
instancetype : 新分配的數組,包括來自對象的第一個計數對象。 返回的對象可能與原始接收者不同。
*/
- (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
/*
返回從給定的unarchiver中的數據初始化的對象。你通常從initWithCoder返回自我: 如果你有一個提前的需求,需要在解碼后替換一個不同的對象,你可以在方法awakeAfterUsingCoder :中做這些.
*/
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;@end
復制代碼@interface NSArray<ObjectType> (NSExtendedArray)
/*
返回一個新的數組,該數組是接收數組與給定的對象添加到結尾形成新數組的copy(復制)。如果anObject為空,則引發NSInvalidArgumentException異常。
*/
- (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject;
/*
返回一個新的數組,它是接收數組的copy,會將其他數組中包含的對象添加到結尾。
*/
- (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType> *)otherArray;
/*
構造并返回NSString對象,該對象是在數組元素之間插入給定分隔符的結果。 如果數組沒有元素,則返回表示空字符串的NSString對象。注意:separator 可以是@"";
*/
- (NSString *)componentsJoinedByString:(NSString *)separator;
/*
返回一個布爾值,該值指示數組中是否存在給定的對象。
從索引0開始,檢查數組中的每個元素與anObject是否相等,直到找到匹配或到達數組的末尾。 如果isEqual對象被認為是相等的:返回YES。
*/
- (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;
/*
*/
- (void)getObjects:(ObjectType _Nonnull __unsafe_unretained [_Nonnull])objects range:(NSRange)range NS_SWIFT_UNAVAILABLE("Use 'subarrayWithRange()' instead");
/*
*/
- (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 API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
@property (nullable, nonatomic, readonly) ObjectType lastObject;
- (NSEnumerator<ObjectType> *)objectEnumerator;
- (NSEnumerator<ObjectType> *)reverseObjectEnumerator;
@property (readonly, copy) NSData *sortedArrayHint;
/*
*/
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context;
/*
*/
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
/*
*/
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;
/*
*/
- (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range;
/* Serializes this instance to the specified URL in the NSPropertyList format (using NSPropertyListXMLFormat_v1_0). For other formats use NSPropertyListSerialization directly. */
/*
*/
- (BOOL)writeToURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));/*
*/
- (void)makeObjectsPerformSelector:(SEL)aSelector NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
/*
*/
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
/*
*/
- (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes;
/*
*/
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
/*
*/
- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSUInteger)indexOfObjectPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.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 NS_NOESCAPE)cmp API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); // binary search@end
復制代碼@interface NSArray<ObjectType> (NSArrayCreation)
/*
*/
+ (instancetype)array;
/*
*/
+ (instancetype)arrayWithObject:(ObjectType)anObject;
/*
*/
+ (instancetype)arrayWithObjects:(const ObjectType _Nonnull [_Nonnull])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;/* Reads array stored in NSPropertyList format from the specified url. */
/*
*/
- (nullable NSArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));/* Reads array stored in NSPropertyList format from the specified url. */
/*
*/
+ (nullable NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)) NS_SWIFT_UNAVAILABLE("Use initializer instead");@end
復制代碼@interface NSArray<ObjectType> (NSDeprecated)/*這種方法是不安全的,因為它可能會導致緩沖區溢出。 您應該使用-getObjects:range:來代替。
*/
- (void)getObjects:(ObjectType _Nonnull __unsafe_unretained [_Nonnull])objects NS_SWIFT_UNAVAILABLE("Use 'as [AnyObject]' instead") API_DEPRECATED("Use -getObjects:range: instead", macos(10.0, 10.13), ios(2.0, 11.0), watchos(2.0, 4.0), tvos(9.0, 11.0));/* 這些方法已被棄用,并將在后續版本中標記為API_DEPRECATED。使用方法的變體 */
+ (nullable NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url;
- (nullable NSArray<ObjectType> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;@end
復制代碼/**************** Mutable Array ****************/@interface NSMutableArray<ObjectType> : NSArray<ObjectType>
/*
*/
- (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;@end
復制代碼@interface NSMutableArray<ObjectType> (NSExtendedMutableArray)/*
*/
- (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 API_DEPRECATED("Not supported", macos(10.0,10.6), ios(2.0,4.0), watchos(2.0,2.0), tvos(9.0,9.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 (NS_NOESCAPE *)(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 API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
/*
*/
- (void)sortUsingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
/*
*/
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));@end
復制代碼@interface NSMutableArray<ObjectType> (NSMutableArrayCreation)
/*
*/
+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;
/*
*/
+ (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;
/*
*/
+ (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url;
/*
*/
- (nullable NSMutableArray<ObjectType> *)initWithContentsOfFile:(NSString *)path;
/*
*/
- (nullable NSMutableArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;@end
復制代碼
總結
以上是生活随笔為你收集整理的iOS 深入解析之NSArray的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】Android Camera 相机
- 下一篇: 大型网站技术架构(二)--大型网站架构演