首页 > 其他 > 详细

Objective--C的Foundation frame之NSMutableArray代码

时间:2016-08-08 14:28:29      阅读:262      评论:0      收藏:0      [点我收藏+]
 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 
 5 @property (nonatomic, strong)NSString *personName;
 6 - (id)initWithName:(NSString *)name;
 7 
 8 @end
 9 
10 #import "Person.h"
11 
12 @implementation Person
13 
14 - (id)initWithName:(NSString *)name
15 {
16     if (self = [super init])
17     {
18         _personName = name;
19     }
20     return self;
21 }
22 
23 @end
24 
25 #import <Foundation/Foundation.h>
26 #import "Person.h"
27 int main(int argc, const char * argv[])
28 {
29     @autoreleasepool
30     {
31         Person *p1 = [[Person alloc] initWithName:@"张三"];
32         Person *p2 = [[Person alloc] initWithName:@"李四"];
33         Person *p3 = [[Person alloc] initWithName:@"王五"];
34         NSArray *personArray = [[NSArray alloc] initWithObjects:p2, p3, nil];
35         
36         NSMutableArray *array = [[NSMutableArray alloc] init];
37         //添加元素
38         [array addObject:p1];
39         [array addObjectsFromArray:personArray];
40         NSLog(@"%@", array);
41         //删除元素
42         //删除数组内所有的元素
43 //        [array removeAllObjects];
44         //删除数组最后一个元素
45 //        [array removeLastObject];
46         //删除指定元素
47         [array removeObject:p2];
48         //删除指定下标的元素(注意数组内元素的个数下标问题会导致崩溃)
49 //        [array removeObjectAtIndex:2];
50         NSLog(@"2.%@", array);
51         
52         //交换元素的位置
53         [array exchangeObjectAtIndex:0 withObjectAtIndex:1];
54         NSLog(@"%@", array);
55     }
56     return 0;
57 }

 

Objective--C的Foundation frame之NSMutableArray代码

原文:http://www.cnblogs.com/songlei0601/p/5748937.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!