[英]Adding objects in Multidimensional NSMutableArray
我對如何在多維數組中添加對象感到困惑。
初始化多維數組是否只與一個簡單的數組相同?
這是我的初始化。
testList = [[NSMutableArray alloc] init];
我需要做點什么
testList[i][j] = item;
我試過了
[[[testList objectAtIndex:i]objectAtIndex:j] addObject:item];
但它似乎沒有工作:(
你添加了很多C來做到這一點。 這對於了解NSMutableArray如何工作以及與C / C ++中已知的2D數組相比有何不同非常重要。
在可變數組中,您可以存儲另一個數組。 例如:
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];
所以現在你的第二個包含NSStrings , 第一個包含第二個 。 現在你可以按照你想要的方式循環。
----編輯:如果你想要1,0你只需要第二個 NSMutableArray的另一個實例。 例如,你有這個數組:
所以這里你將在第二個數組中有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協議來執行此操作。
如果需要固定大小的數組,則使用純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];
為了擴展@ 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上寫了更多詳細信息。
[[testList objectAtIndex:i] insertObject:item atIndex:j];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.