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 }

 *************补充NSArray***********

 1 #import <Foundation/Foundation.h>
 2 
 3 int main(int argc, const char * argv[])
 4 {
 5     @autoreleasepool
 6     {
 7         //  OC的数组可以存储不同类型的对象,OC的数组只能存储对象
 8         NSArray *array1 = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", nil];
 9         //  数组的长度
10         int count = (int)array1.count;
11         NSLog(@"count = %d", count);
12         //判断数组中是否包含对应的对象
13         BOOL isHave = [array1 containsObject:@"c"];
14         if (isHave)
15         {
16             NSLog(@"存在");
17         }
18         else
19         {
20             NSLog(@"不存在");
21         }
22         //取得数组中的最后一个元素
23         NSString *str = [array1 lastObject];
24         NSLog(@"str = %@", str);
25         //取首元素
26         str = [array1 firstObject];
27         NSLog(@"str = %@", str);
28         //取出下标为2的元素****
29         str = [array1 objectAtIndex:2];
30         NSLog(@"str = %@", str);
31         //打印元素所对应的下标(如果元素不存在则打印-1的值)
32         int index = (int)[array1 indexOfObject:@"d"];
33         NSLog(@"index = %d", index);
34         
35         //数组的遍历(1.基本的for循环通过下标逐一取出查看。2.for in 快速枚举。3.枚举器(迭代器))
36         for(int i = 0; i < count; ++i)//int count = (int)array1.count;
37         {
38             NSLog(@"%@", array1[i]);
39         }
40         //或者
41         for(int i = 0; i <count; ++i)
42         {
43             NSString *str1 = [array1 objectAtIndex:i];
44             NSLog(@"str1 = %@", str1);
45         }
46         //2.(如果使用快速枚举,我们需要让数组中元素的类型保持一致)
47         for(NSString *str2 in array1)
48         {
49             NSLog(@"str2 = %@", str2);
50         }
51         //3.枚举器
52         for(id object in array1)
53         {
54             NSLog(@"object = %@", object);
55         }
56         
57         return 0;
58     }
59 }
 1 2016-08-10 15:32:58.788 NSArray[1308:191388] count = 5
 2 2016-08-10 15:32:58.789 NSArray[1308:191388] 存在
 3 2016-08-10 15:32:58.789 NSArray[1308:191388] str = e
 4 2016-08-10 15:32:58.789 NSArray[1308:191388] str = a
 5 2016-08-10 15:32:58.789 NSArray[1308:191388] str = c
 6 2016-08-10 15:32:58.789 NSArray[1308:191388] index = 3
 7 2016-08-10 15:32:58.789 NSArray[1308:191388] a
 8 2016-08-10 15:32:58.789 NSArray[1308:191388] b
 9 2016-08-10 15:32:58.789 NSArray[1308:191388] c
10 2016-08-10 15:32:58.790 NSArray[1308:191388] d
11 2016-08-10 15:32:58.790 NSArray[1308:191388] e
12 2016-08-10 15:32:58.790 NSArray[1308:191388] str1 = a
13 2016-08-10 15:32:58.790 NSArray[1308:191388] str1 = b
14 2016-08-10 15:32:58.790 NSArray[1308:191388] str1 = c
15 2016-08-10 15:32:58.790 NSArray[1308:191388] str1 = d
16 2016-08-10 15:32:58.790 NSArray[1308:191388] str1 = e
17 2016-08-10 15:32:58.790 NSArray[1308:191388] str2 = a
18 2016-08-10 15:32:58.790 NSArray[1308:191388] str2 = b
19 2016-08-10 15:32:58.790 NSArray[1308:191388] str2 = c
20 2016-08-10 15:32:58.790 NSArray[1308:191388] str2 = d
21 2016-08-10 15:32:58.790 NSArray[1308:191388] str2 = e
22 2016-08-10 15:32:58.790 NSArray[1308:191388] object = a
23 2016-08-10 15:32:58.791 NSArray[1308:191388] object = b
24 2016-08-10 15:32:58.791 NSArray[1308:191388] object = c
25 2016-08-10 15:32:58.791 NSArray[1308:191388] object = d
26 2016-08-10 15:32:58.791 NSArray[1308:191388] object = e
27 Program ended with exit code: 0