IOS学习笔记十九NSArray和NSMutableArray

举报
chenyu 发表于 2021/07/27 00:01:25 2021/07/27
【摘要】 1、NSArray NSArray不可变集合,不能添加新元素和删除已有元素和替换元素       2、demo Dog.h #import <Foundation/Foundation.h>#ifndef Dog_h#define Dog_h @interface Dog : NSObject@property (non...

1、NSArray

NSArray不可变集合,不能添加新元素和删除已有元素和替换元素

 

 

 

2、demo

Dog.h


  
  1. #import <Foundation/Foundation.h>
  2. #ifndef Dog_h
  3. #define Dog_h
  4. @interface Dog : NSObject
  5. @property (nonatomic, strong) NSString *name;
  6. @property (nonatomic, assign) int age;
  7. -(id)initWithName:(NSString *)name age:(int)age;
  8. -(void)say:(NSString *)content;
  9. @end
  10. #endif /* Dog_h */

 

Dog.m

 


  
  1. #import <Foundation/Foundation.h>
  2. #import "Dog.h"
  3. @implementation Dog
  4. @synthesize name;
  5. @synthesize age;
  6. -(id)initWithName:(NSString *)name age:(int)age
  7. {
  8. if (self = [super init])
  9. {
  10. self.name = name;
  11. self.age = age;
  12. }
  13. return self;
  14. }
  15. -(void)say:(NSString *)content
  16. {
  17. NSLog(@"%@ say %@", name, content);
  18. }
  19. -(BOOL)isEqual:(id)object
  20. {
  21. if (object == self)
  22. return YES;
  23. if ([object class] == Dog.class)
  24. {
  25. Dog *dog = (Dog *)object;
  26. return [self.name isEqualToString:dog.name] && (self.age == dog.age);
  27. }
  28. return NO;
  29. }
  30. @end

 

 

 

main.m


  
  1. int main(int argc, char * argv[]) {
  2. @autoreleasepool {
  3. NSArray *array = [NSArray arrayWithObjects:@"chenyu", @"hello", @"word", @"god", nil];
  4. NSLog(@"first data id %@", [array objectAtIndex:0]);
  5. NSLog(@"first data id %@", [array objectAtIndex:1]);
  6. NSLog(@"first data id %@", [array lastObject]);
  7. NSLog(@"@hello is %ld", [array indexOfObject:@"hello"]);
  8. array = [array arrayByAddingObject:@"chenzixuan"];
  9. for (int i = 0; i < array.count; ++i)
  10. {
  11. NSLog(@"%@", [array objectAtIndex:i]);
  12. }
  13. NSArray *arr = [NSArray arrayWithObjects:[[Dog alloc] initWithName:@"chen" age:1], [[Dog alloc] initWithName:@"chenyu" age:1], [[Dog alloc] initWithName:@"chengongyu" age:1], nil];
  14. Dog *d = [[Dog alloc] initWithName:@"chenyu" age:1];
  15. NSInteger pos = [arr indexOfObject:d];
  16. NSLog(@"index is %ld", pos);
  17. [arr makeObjectsPerformSelector:@selector(say:) withObject:@"拜拜"];
  18. NSString *str = @"hello";
  19. [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  20. NSLog(@"正在处理第%ld个元素:%@", idx, obj);
  21. }];
  22. }
  23. }

 

 

 

3、结果


  
  1. 2018-07-15 21:17:44.447732+0800 cyTest[31047:9530812] first data id chenyu
  2. 2018-07-15 21:17:44.449642+0800 cyTest[31047:9530812] first data id hello
  3. 2018-07-15 21:17:44.450020+0800 cyTest[31047:9530812] first data id god
  4. 2018-07-15 21:17:44.450943+0800 cyTest[31047:9530812] @hello is 1
  5. 2018-07-15 21:17:44.451207+0800 cyTest[31047:9530812] chenyu
  6. 2018-07-15 21:17:44.451493+0800 cyTest[31047:9530812] hello
  7. 2018-07-15 21:17:44.451739+0800 cyTest[31047:9530812] word
  8. 2018-07-15 21:17:44.451986+0800 cyTest[31047:9530812] god
  9. 2018-07-15 21:17:44.452233+0800 cyTest[31047:9530812] chenzixuan
  10. 2018-07-15 21:17:44.453020+0800 cyTest[31047:9530812] index is 1
  11. 2018-07-15 21:17:44.453332+0800 cyTest[31047:9530812] chen say 拜拜
  12. 2018-07-15 21:17:44.453581+0800 cyTest[31047:9530812] chenyu say 拜拜
  13. 2018-07-15 21:17:44.453803+0800 cyTest[31047:9530812] chengongyu say 拜拜
  14. 2018-07-15 21:17:44.454188+0800 cyTest[31047:9530812] 正在处理第0个元素:chenyu
  15. 2018-07-15 21:17:44.454456+0800 cyTest[31047:9530812] 正在处理第1个元素:hello
  16. 2018-07-15 21:17:44.455253+0800 cyTest[31047:9530812] 正在处理第2个元素:word
  17. 2018-07-15 21:17:44.478747+0800 cyTest[31047:9530812] 正在处理第3个元素:god
  18. 2018-07-15 21:17:44.478861+0800 cyTest[31047:9530812] 正在处理第4个元素:chenzixuan

 

 

 

 

 

 

4、NSMutableArray

nsMutableArray可变数组,代表是一个集合元素可变的集合

一般有add开头、 remove开头、replace开头、sort开头的方法

 

 

 

 

 

5、测试Demo


  
  1. NSMutableArray *muArray = [NSMutableArray arrayWithObjects:@"chen", @"yu", @"hello", @"word", nil];
  2. [muArray addObject:@"123"];
  3. [muArray removeObject:@"yu"];
  4. [muArray replaceObjectAtIndex:1 withObject:@"xx"];
  5. for (id ob in muArray) {
  6. NSLog(@"ob is %@", ob);
  7. }

 

 

 

 

6、结果


  
  1. 2018-07-18 22:02:57.498997+0800 cyTest[31429:12701122] ob is chen
  2. 2018-07-18 22:02:57.600036+0800 cyTest[31429:12701122] ob is xx
  3. 2018-07-18 22:02:57.600599+0800 cyTest[31429:12701122] ob is word
  4. 2018-07-18 22:02:57.600824+0800 cyTest[31429:12701122] ob is 123

 

 

 

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/81057159

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。