您好,登錄后才能下訂單哦!
數組對象NSArray
數組對象的創建:
+ (instancetype)array //空數組 + (instancetype)arrayWithObject:(ObjectType)anObject //一個對象元素的數組 + (instancetype)arrayWithObjects:(ObjectType)firstObj, … //多個對象元素的數組(最后一個參數為nil)
字面值:
NSArray * arr = @[@"hello", @10, @YES]; //常量方式
注意:只能存儲OC對象,數值需要封裝成對象,nil需要封裝成NSNull
基本操作:
@property(readonly) NSUInteger count - (BOOL)containsObject:(ObjectType)anObject @property(nonatomic, readonly) ObjectType firstObject @property(nonatomic, readonly) ObjectType lastObject - (ObjectType)objectAtIndex:(NSUInteger)index - (NSUInteger)indexOfObject:(ObjectType)anObject
文件/URL操作相關:
+ (NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)aPath + (NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)aURL - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag - (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag
數組的遍歷方式一:C語言方式
for ( int i=0; i<arr.count; i++) { NSLog(@"%@", arr[i]); }
數組的遍歷方式二:
for ( id obj in arr4) { NSUInteger i = [arr4 indexOfObject:obj]; NSLog(@"index:%lu %@", i, obj); }
注意:這種遍歷方式應用在可變數組時,不要在 遍歷過程中改變數組
可變數組對象
NSMutableArray用于描述可變數組對象,是NSArray的子類
添加:
- (void)addObject:(ObjectType)anObject - (void)addObjectsFromArray:(NSArray<ObjectType> *)otherArray - (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index
刪除:
- (void)removeAllObjects - (void)removeLastObject - (void)removeObjectAtIndex:(NSUInteger)index
集合對象
NSSet用于描述集合對象,子類為NSMutableSet
集合與數組的區別:1)集合不支持下標取成員 2)集合中不能有重復對象
集合對象的創建:
+ (instancetype)set + (instancetype)setWithArray:(NSArray<ObjectType> *)array + (instancetype)setWithObject:(ObjectType)object + (instancetype)setWithObjects:(ObjectType)firstObj, firstObj, ...
集合對象的常見操作:
@property(readonly) NSUInteger count @property(readonly, copy) NSArray < ObjectType > *allObjects - (ObjectType)anyObject - (BOOL)containsObject:(ObjectType)anObject
可變集合對象的操作:
- (void)addObject:(ObjectType)object - (void)removeObject:(ObjectType)object - (void)removeAllObjects - (void)addObjectsFromArray:(NSArray<ObjectType> *)array
其他集合對象:
可變集合對象NSMutableSet
索引集合NSIndexSet
字符集合NSCharacterSet
...
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。