1、NSArray
NSArray不可變集合,不能添加新元素和删除已有元素和替換元素
2、demo
Dog.h
#import <Foundation/Foundation.h>
#ifndef Dog_h
#define Dog_h
@interface Dog : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int age;
-(id)initWithName:(NSString *)name age:(int)age;
-(void)say:(NSString *)content;
@end
#endif /* Dog_h */
Dog.m
#import <Foundation/Foundation.h>
#import "Dog.h"
@implementation Dog
@synthesize name;
@synthesize age;
-(id)initWithName:(NSString *)name age:(int)age
{
if (self = [super init])
{
self.name = name;
self.age = age;
}
return self;
}
-(void)say:(NSString *)content
{
NSLog(@"%@ say %@", name, content);
}
-(BOOL)isEqual:(id)object
{
if (object == self)
return YES;
if ([object class] == Dog.class)
{
Dog *dog = (Dog *)object;
return [self.name isEqualToString:dog.name] && (self.age == dog.age);
}
return NO;
}
@end
main.m
int main(int argc, char * argv[]) {
@autoreleasepool {
NSArray *array = [NSArray arrayWithObjects:@"chenyu", @"hello", @"word", @"god", nil];
NSLog(@"first data id %@", [array objectAtIndex:0]);
NSLog(@"first data id %@", [array objectAtIndex:1]);
NSLog(@"first data id %@", [array lastObject]);
NSLog(@"@hello is %ld", [array indexOfObject:@"hello"]);
array = [array arrayByAddingObject:@"chenzixuan"];
for (int i = 0; i < array.count; ++i)
{
NSLog(@"%@", [array objectAtIndex:i]);
}
NSArray *arr = [NSArray arrayWithObjects:[[Dog alloc] initWithName:@"chen" age:1], [[Dog alloc] initWithName:@"chenyu" age:1], [[Dog alloc] initWithName:@"chengongyu" age:1], nil];
Dog *d = [[Dog alloc] initWithName:@"chenyu" age:1];
NSInteger pos = [arr indexOfObject:d];
NSLog(@"index is %ld", pos);
[arr makeObjectsPerformSelector:@selector(say:) withObject:@"拜拜"];
NSString *str = @"hello";
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"正在處理第%ld個元素:%@", idx, obj);
}];
}
}
3、結果
2018-07-15 21:17:44.447732+0800 cyTest[31047:9530812] first data id chenyu
2018-07-15 21:17:44.449642+0800 cyTest[31047:9530812] first data id hello
2018-07-15 21:17:44.450020+0800 cyTest[31047:9530812] first data id god
2018-07-15 21:17:44.450943+0800 cyTest[31047:9530812] @hello is 1
2018-07-15 21:17:44.451207+0800 cyTest[31047:9530812] chenyu
2018-07-15 21:17:44.451493+0800 cyTest[31047:9530812] hello
2018-07-15 21:17:44.451739+0800 cyTest[31047:9530812] word
2018-07-15 21:17:44.451986+0800 cyTest[31047:9530812] god
2018-07-15 21:17:44.452233+0800 cyTest[31047:9530812] chenzixuan
2018-07-15 21:17:44.453020+0800 cyTest[31047:9530812] index is 1
2018-07-15 21:17:44.453332+0800 cyTest[31047:9530812] chen say 拜拜
2018-07-15 21:17:44.453581+0800 cyTest[31047:9530812] chenyu say 拜拜
2018-07-15 21:17:44.453803+0800 cyTest[31047:9530812] chengongyu say 拜拜
2018-07-15 21:17:44.454188+0800 cyTest[31047:9530812] 正在處理第0個元素:chenyu
2018-07-15 21:17:44.454456+0800 cyTest[31047:9530812] 正在處理第1個元素:hello
2018-07-15 21:17:44.455253+0800 cyTest[31047:9530812] 正在處理第2個元素:word
2018-07-15 21:17:44.478747+0800 cyTest[31047:9530812] 正在處理第3個元素:god
2018-07-15 21:17:44.478861+0800 cyTest[31047:9530812] 正在處理第4個元素:chenzixuan
4、NSMutableArray
nsMutableArray可變數組,代表是一個集合元素可變的集合
一般有add開頭、 remove開頭、replace開頭、sort開頭的方法
5、測試Demo
NSMutableArray *muArray = [NSMutableArray arrayWithObjects:@"chen", @"yu", @"hello", @"word", nil];
[muArray addObject:@"123"];
[muArray removeObject:@"yu"];
[muArray replaceObjectAtIndex:1 withObject:@"xx"];
for (id ob in muArray) {
NSLog(@"ob is %@", ob);
}
6、結果
2018-07-18 22:02:57.498997+0800 cyTest[31429:12701122] ob is chen
2018-07-18 22:02:57.600036+0800 cyTest[31429:12701122] ob is xx
2018-07-18 22:02:57.600599+0800 cyTest[31429:12701122] ob is word
2018-07-18 22:02:57.600824+0800 cyTest[31429:12701122] ob is 123