標籤:
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 Person13 14 - (id)initWithName:(NSString *)name15 {16 if (self = [super init])17 {18 _personName = name;19 }20 return self;21 }22 23 @end24 25 #import <Foundation/Foundation.h>26 #import "Person.h"27 int main(int argc, const char * argv[])28 {29 @autoreleasepool30 {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代碼