2013-08-22 37 views
0

我有被動態創建2個陣列。我在網上讀過一些例子,我完全不能理解它。我看到的是,陣列似乎是1維而不是2.如何創建一個二維數組目標C

下面的代碼將一些對象分配給數組「combineObjectIssues」,然後將其添加到「combineAll」中以獲得2D數組。我想要「currentObject.date」爲索引0,而「issuesDiscovered」數組爲索引1.

for (currentObject in currentObjects) { 
     [combineObjectIssues addObject:currentObject.date]; //2D Array Row 
     for (Issue *checkIssue in currentObject.issuesDiscovered) { 
      if (checkIssue) { 
       [issuesDiscovered addObject:checkIssue]; 
      } 
     } 
     [tempIssues addObject:[issuesDiscovered copy]]; // to combine all array of issues 
     [combineOjectIssues addObjectsFromArray:[issuesDiscovered copy]]; //2D Array column 
     [combineAll addObject:[combineObjectIssues copy]]; 
     [issuesDiscovered removeAllObjects]; //remove all objects; 
     [combineObjectIssues removeAllObjects]; //remove all objects 
    } 
} 

下面是我的combineAll數組輸出。

(
    (
    "2013-07-19 09:00:00", 
    "<Issue: 0x8c171f0>", 
    "<Issue: 0x8c16e50>", 
    "<Issue: 0x8c16d30>", 
    "<Issue: 0x8c16a10>", 
    "<Issue: 0x8c16090>", 
    "<Issue: 0x8c15bb0>", 
    "<Issue: 0x8c156d0>" 
), 
    (
    "2013-07-13 14:30:00" 
), 
    (
    "2013-06-08 14:30:00", 
    "<Issue: 0x8c10340>", 
    "<Issue: 0x8c0fad0>", 
    "<Issue: 0x8c0f590>", 
    "<Issue: 0x8c0f0c0>" 
), 
    (
    "2013-05-04 11:30:00" 
) 
) 

正如你從輸出中看到的那樣,它是一個我不想要的1維數組。我想要的是[0] [0],它包含日期,對於[0] [1],它包含一系列問題。

我知道我的代碼有可能是不對的。因此,請協助我。感謝您的幫助。

回答

2

試試這個:

你總是可以得到這樣的值,但語法可以是不同的。

[[combineAll objectAtIndex:0] objectAtIndex:1]; 

這樣你可以得到[0] [1]的值。你可以這樣做,使它成爲二維數組。

你也可以這樣做:

NSString* str = combineAll[0][1]; 

爲了你能做到這一點的問題數據:

NSArray* issueArray = combineAll[0]; 

這將在combineAll數組的0位置返回的所有問題。

希望它有助於!

+1

您可以使用字面語法來提高可讀性:'combineAll [0] [1];'是等效的。 –

+0

我做到了。但它只抓取<問題:0x8c171f0>而不是包含[0] [1]的7個問題的問題陣列。 –

+0

它只會返回一個值,因爲在0位置有8個對象的數組。現在你已經調用了[0] [1],這意味着它將從該8值數組中返回編號爲1的對象。 –

1

多維數據結構來存儲Cocoa對象的問題頻繁出現。有時候可以通過以更適合概念模型的方式重新思考對象層次結構來解決問題。但有時多維數組的最佳機制。普通的C數組可以存儲指向Cocoa對象的指針,並且可能是處理此問題的更好方法。舉例來說:

#import <Foundation/Foundation.h> 

@interface Foo : NSObject 
@property (nonatomic,strong) NSString *name; 
@end 

@implementation Foo 
@end 

int main(int argc, const char * argv[]) 
{ 
    @autoreleasepool { 
     id p[10][10]; 
     // create a bunch of foos 
     for(uint8_t i = 0; i < 10; i++) { 
      for(uint8_t j = 0; j < 10 ; j++) { 
       Foo *aFoo = [[Foo alloc] init]; 
       aFoo.name = [NSString stringWithFormat:@"Foo_%02d_%02d",i,j]; 
       p[i][j] = aFoo; 
      } 
     } 
     // show that we can recover a Foo from C array 
     Foo *someFoo = (Foo *)p[5][5]; 
     NSLog(@"Foo[5][5] = %@",someFoo.name); 
     // prints Foo[5][5] = Foo_05_05 to the console 
    } 
    return 0; 
} 

或者,如果你想動態地分配C數組,你將有更多的工作要做。見this gist

0

你會使用這種數據結構,使生活苛責自己。你必須記住第一個索引與其他索引是不同的,並且總是必須做索引移位等等。但是,如果您必須嘗試如下所示:

NSMutableArray *output = [NSMutableArray array]; 
for (YourClass *currentObject in currentObjects) { 
    //Making assumption currentObject.issuesDiscovered in an NSArray! if it's not you'll need to initialize the second element differently 
    NSArray *arrayForObject = @[currentObject.date,[currentObject.issuesDiscovered copy]]; 
    [output addObject:arrayForObject]; 
}