IOS学习笔记十九NSArray和NSMutableArray
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
赞 (0)