2014-02-21 61 views
0

我是iOS新手,初學者是關於編程的。多值集索引名稱

我對編程有一些疑問。

問題

我有5 NSMutableArray裏,我想所有的NSMutableArray添加到1 NSMutableArray的

按照本準則。

在.h文件中

@property (strong, nonatomic) NSMutableArray *orderType1; 
    @property (strong, nonatomic) NSMutableArray *orderType2; 
    @property (strong, nonatomic) NSMutableArray *orderType3; 
    @property (strong, nonatomic) NSMutableArray *orderType4; 
    @property (strong, nonatomic) NSMutableArray *orderType5; 
    @property (strong, nonatomic) NSMutableArray *allData; 

在.m文件

for(int i = 0; i < orderCount; i++) // orderCount is count of orderType(count = 5) 
    { 
     // Do something about condition to get Data... 
     // ... This Condition will Receive "data" 

     // Add Object 
     if (i == 0) 
      [_orderType1 addObject:data]; 
     else if (i == 1) 
      [_orderType2 addObject:data]; 
     else if (i == 2) 
      [_orderType3 addObject:data]; 
     else if (i == 3) 
      [_orderType4 addObject:data]; 
     else 
      [_orderType5 addObject:data]; 
    } 

    _allData = [NSArray arrayWithObjects:_orderType1, _orderType2, _orderType3, _orderType4, _orderType5, nil]; 

結果這個代碼是工作和正確的。但我想要索引i的_orderType的名稱。

例如:在循環

for(int i = 0; i < orderCount; i++) // orderCount is count of orderType(count = 5) 
{ 
     // Do something about condition to get Data... 
     // ... This Condition will Receive "data" 

     // Add Object 
     [_orderType[i] addObject:data]  
} 

如何解決,或者我不能在這種情況下解決這個問題。

謝謝你,並對我的錯誤感到抱歉。

+0

你有沒有想過用* allData代替數組使用NSMutableDictionary? –

回答

0

我想這是你想要做什麼:

self.orderType1 = [[NSMutableArray alloc] init]; 
self.orderType2 = [[NSMutableArray alloc] init]; 
self.orderType3 = [[NSMutableArray alloc] init]; 
self.orderType4 = [[NSMutableArray alloc] init]; 
self.orderType5 = [[NSMutableArray alloc] init]; 

self.allData = [NSArray arrayWithObjects:self.orderType1, self.orderType2, self.orderType3, self.orderType4, self.orderType5, nil]; 

for (int i = 0; i < orderCount; i++) { 
    [self.allData[i] addObject:data]; 
} 

你可能要避免使用像_orderType1語法,因爲這是直接訪問變量,而不是使用一個getter和setter方法。相反,使用self.orderType1