Just My Life & My Work

Archive for 2012 年 10 月 04 日

[Objective C] NSMutableArray

對應NSArray,接著來練習NSMutableArray!屬於可變 (mutable) 的物件種類,裡頭可以放任何的型態的物件。

常用方法如下:

  • -(void) addObject: (id) anObject
    將 anObject 加入陣列
  • -(void) removeObject: (id)anObject
    將陣列內所有符合 anObject 的物件刪除
  • -(void) sortUsingSelector: (SEL) comparator
    以 comparator 的方法,將陣列的元素加以排序
  • -(void) replaceObjectAtIndex: (NSUInteger) index withObject: (id) anObject
    將陣列索引值為 index 的元素取代為 anObject
  • -(void) exchangeObjectAtIndex: (NSUInteger) idx1 withObjectAtIndex: (NSUInteger) idx2
    將陣列索引值為 idx1 與 idx2 的元素調換

範例程式碼:


//
//  main.m
//  HappyMan's Program - NSMutableArray
//  Xcode 4.5
//  Created by ShengWen on 12/9/26.
//  Copyright (c) 2012年 ShengWen. All rights reserved.
//  https://cg2010studio.wordpress.com/

#import <Foundation/Foundation.h>
@interface monster : NSObject
{
    int life;
}
@property int life;
-(void) show;
@end

@interface posion : NSObject
{
    int cure;
}
@property int cure;
-(void) show;
@end

@implementation monster
@synthesize life;
-(void) show{
    NSLog(@"life = %i",life);
}
@end

@implementation posion
@synthesize cure;
-(void) show{
    NSLog(@"cure = %i",cure);
}

@end

int main (int argc, const char * argv[]) {

    monster* lion1 = [[monster alloc] init];
    monster* lion2 = [[monster alloc] init];
    monster* lion3 = [[monster alloc] init];
    posion* red1 = [[posion alloc] init];
    posion* red2 = [[posion alloc] init];
    posion* red3 = [[posion alloc] init];
    NSMutableArray* mArray = [[NSMutableArray alloc] init];

    lion1.life = 99;
    lion2.life = 199;
    lion3.life = 299;
    red1.cure = 49;
    red2.cure = 149;
    red3.cure = 249;

    // 將物件加入陣列中
    [mArray addObject:lion1];
    [mArray addObject:lion2];
    [mArray addObject:lion3];
    [mArray addObject:red1];
    [mArray addObject:red2];
    [mArray addObject:red3];

    // 使用陣列內物件的方法
    NSLog(@"使用物件方法:");
    [mArray makeObjectsPerformSelector:@selector(show)];

    // 刪除物件
    NSLog(@"刪除物件:");
    [mArray removeObject:red2];
    [mArray makeObjectsPerformSelector:@selector(show)];

    // 取代物件
    NSLog(@"取代物件:");
    [mArray replaceObjectAtIndex:2 withObject:red3];
    [mArray makeObjectsPerformSelector:@selector(show)];

    // 對調物件
    NSLog(@"對調物件:");
    [mArray exchangeObjectAtIndex:0 withObjectAtIndex:2];
    [mArray makeObjectsPerformSelector:@selector(show)];

    return 0;
}

執行結果:

2012-10-04 23:51:42.926 HappyMan’s First Program[8898:403] 使用物件方法:

2012-10-04 23:51:42.928 HappyMan’s First Program[8898:403] life = 99

2012-10-04 23:51:42.929 HappyMan’s First Program[8898:403] life = 199

2012-10-04 23:51:42.929 HappyMan’s First Program[8898:403] life = 299

2012-10-04 23:51:42.930 HappyMan’s First Program[8898:403] cure = 49

2012-10-04 23:51:42.930 HappyMan’s First Program[8898:403] cure = 149

2012-10-04 23:51:42.931 HappyMan’s First Program[8898:403] cure = 249

2012-10-04 23:51:42.931 HappyMan’s First Program[8898:403] 刪除物件:

2012-10-04 23:51:42.932 HappyMan’s First Program[8898:403] life = 99

2012-10-04 23:51:42.932 HappyMan’s First Program[8898:403] life = 199

2012-10-04 23:51:42.933 HappyMan’s First Program[8898:403] life = 299

2012-10-04 23:51:42.933 HappyMan’s First Program[8898:403] cure = 49

2012-10-04 23:51:42.934 HappyMan’s First Program[8898:403] cure = 249

2012-10-04 23:51:42.934 HappyMan’s First Program[8898:403] 取代物件:

2012-10-04 23:51:42.935 HappyMan’s First Program[8898:403] life = 99

2012-10-04 23:51:42.935 HappyMan’s First Program[8898:403] life = 199

2012-10-04 23:51:42.936 HappyMan’s First Program[8898:403] cure = 249

2012-10-04 23:51:42.936 HappyMan’s First Program[8898:403] cure = 49

2012-10-04 23:51:42.937 HappyMan’s First Program[8898:403] cure = 249

2012-10-04 23:51:42.937 HappyMan’s First Program[8898:403] 對調物件:

2012-10-04 23:51:42.938 HappyMan’s First Program[8898:403] cure = 249

2012-10-04 23:51:42.940 HappyMan’s First Program[8898:403] life = 199

2012-10-04 23:51:42.940 HappyMan’s First Program[8898:403] life = 99

2012-10-04 23:51:42.941 HappyMan’s First Program[8898:403] cure = 49

2012-10-04 23:51:42.942 HappyMan’s First Program[8898:403] cure = 249

話說,輸出明明就很少,結果大部分的內容都是時間加上專案名稱,下次該把這部分給刪除掉~

參考:Objective-C 陣列物件NSMutableArray Class ReferenceObjective-C 入門指南 – NSMutableArray

中秋團圓烤肉

明月幾時有?把酒問青天……月有陰晴圓缺,人有旦夕禍福~一年一次的中秋團圓烤肉怎能錯過呢?

(繼續閱讀…)

標籤雲

%d 位部落客按了讚: