Objective--C的Foundation frame之NSMutableArray代碼

來源:互聯網
上載者:User

標籤:

 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代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.