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
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值