2012-07-17 116 views
2

我很困惑如何在多維數組中添加對象。在多維NSMutableArray中添加對象

是初始化多維數組是相同的只是一個簡單的數組?

這是我的初始化。

testList = [[NSMutableArray alloc] init]; 

我需要做的是這樣

testList[i][j] = item; 

我試圖

[[[testList objectAtIndex:i]objectAtIndex:j] addObject:item]; 

,但它似乎並沒有工作:(

+0

ý書面進一步的細節你應該在原來的'testList'中添加更多的'NSMutableArray'(或'NSArray')對象,因爲**在objective-c中沒有多維數組**;你可以在KVC中使用'NSMutableDictionary'(或'NSDictionary');或者你可以將它們相互結合;或者你可以使用'NSMutableSet'(或'NSSet'),或者你可以將所有東西與所有東西結合起來......一切都是無限的,它取決於你想表示什麼樣的數據。 – holex 2012-07-17 16:10:23

回答

5

您是添加到多C到這樣做對於知道NSMutableArray是如何工作的以及如何與從C/C++中知道的二維數組相比是非常重要的。

在可變數組中,您可以存儲另一個數組。例如:

NSMutableArray *first = [[NSMutableArray alloc] init]; 
NSMutableArray *second = [[NSMutableArray alloc] init]; 

[first addObject:second]; 

現在你在第一個數組的第一行有數組!這非常類似於C/C++ 2D數組。

所以,如果你想要一些對象添加到「0,0」你這樣做:

NSString *mytest = [[NSString alloc] initWithString:@"test"]; 
[second addObject:mytest]; 
[first addObject:second]; 

所以,現在你包含NSString的第一包含現在你可以像你想要的那樣循環。

----編輯: 如果你想1,0您只需的NSMutableArray的另一個實例。比如你有這樣的數組:

arr

所以在這裏,你將有數組3個元素。

NSMutableArray *first = [[NSMutableArray alloc] init]; 
for(int i =0 ; i < your_size_condition ; i++) {//if you have size, if don't not a problem, you could use while! 
    NSArray *second = [[NSArray alloc] initWithObjects:"@something",@"somethingelse",@"more",nil]; 
    [first addObject:second]; 
} 

您可能需要執行NSCopying protocol來執行此操作。

+0

嗯,我想我明白了你的意思,但是如果我想在1,0加上呢? – 2012-07-17 10:37:54

+0

我更新了我的答案。希望我說清楚。 – Kuba 2012-07-17 10:50:40

+0

解決了它!謝啦! – 2012-07-18 03:56:58

5

如果你需要一個固定大小的數組,然後使用普通的C數組。之前使用動態ObjC陣列需要來創建它:

NSMutableArray* array = [NSMutableArray arrayWithCapacity:N]; 
for(int i=0; i<N; i++) { 
    [array addObject:[NSMutableArray arrayWithCapacity:M]]; 
} 

UPD:以下方法可能會有所幫助這樣的陣列的工作:

[[array objectAtIndex:i] addObject:obj]; 
[[array objectAtIndex:i] insertObject:obj atIndex:j]; 
[[array objectAtIndex:i] replaceObjectAtIndex:j withObject:obj]; 
+0

nope,大小是動態的,我只需要知道如何在NSMutableArray中的testList [i] [n] – 2012-07-17 10:32:45

+0

中添加對象,您不必首先聲明大小,只需添加另一個對象即可。 C++中的列表比數組更多。 – Kuba 2012-07-17 10:34:36

-1
[[testList objectAtIndex:i] insertObject:item atIndex:j]; 
0

要在@ onegray的答案擴大,你可以設置一些類別方法來使軟多維數組更容易處理。

MultiMutableArray.h

@interface NSMutableArray(MultiMutableArray) 
    -(id)objectAtIndex:(int)i subIndex:(int)s; 
    -(void)addObject:(id)o toIndex:(int)i; 
@end 


@implementation NSMutableArray(MultiMutableArray) 

MultiMutableArray.m

#import "MultiMutableArray.h" 

-(id)objectAtIndex:(int)i subIndex:(int)s 
{ 
    id subArray = [self objectAtIndex:i]; 
    return [subArray isKindOfClass:NSArray.class] ? [subArray objectAtIndex:s] : nil; 
} 

-(void)addObject:(id)o toIndex:(int)i 
{ 
    while(self.count <= i) 
     [self addObject:NSMutableArray.new]; 
    NSMutableArray* subArray = [self objectAtIndex:i]; 
    [subArray addObject: o]; 
} 

@end 

例如,MultiArrayTests.m

#import <SenTestingKit/SenTestingKit.h> 

#import "MultiMutableArray.h" 

@interface MultiArrayTests : SenTestCase 
@end 

@implementation MultiArrayTests 

-(void)testMultiArray 
{ 
    NSMutableArray* a = NSMutableArray.new; 
    [a addObject:@"0a" toIndex:0]; 
    [a addObject:@"0b" toIndex:0]; 
    [a addObject:@"0c" toIndex:0]; 
    [a addObject:@"1a" toIndex:1]; 
    [a addObject:@"1b" toIndex:1]; 
    [a addObject:@"2a" toIndex:2]; 
    STAssertEquals(a.count, 3U, nil); 
    NSMutableArray* a1 = [a objectAtIndex:0]; 
    NSMutableArray* a2 = [a objectAtIndex:1]; 
    NSMutableArray* a3 = [a objectAtIndex:2]; 
    STAssertEquals(a1.count, 3U, nil); 
    STAssertEquals(a2.count, 2U, nil); 
    STAssertEquals(a3.count, 1U, nil); 
} 

@end 

我在http://peterdeweese.tumblr.com/post/27411932460/soft-multi-dimensional-arrays-in-objective-c

相關問題