我很困惑如何在多维数组中添加对象。在多维NSMutableArray中添加对象
是初始化多维数组是相同的只是一个简单的数组?
这是我的初始化。
testList = [[NSMutableArray alloc] init];
我需要做的是这样
testList[i][j] = item;
我试图
[[[testList objectAtIndex:i]objectAtIndex:j] addObject:item];
,但它似乎并没有工作:(
我很困惑如何在多维数组中添加对象。在多维NSMutableArray中添加对象
是初始化多维数组是相同的只是一个简单的数组?
这是我的初始化。
testList = [[NSMutableArray alloc] init];
我需要做的是这样
testList[i][j] = item;
我试图
[[[testList objectAtIndex:i]objectAtIndex:j] addObject:item];
,但它似乎并没有工作:(
您是添加到多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的另一个实例。比如你有这样的数组:
所以在这里,你将有秒数组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来执行此操作。
嗯,我想我明白了你的意思,但是如果我想在1,0加上呢? – 2012-07-17 10:37:54
我更新了我的答案。希望我说清楚。 – Kuba 2012-07-17 10:50:40
解决了它!谢啦! – 2012-07-18 03:56:58
如果你需要一个固定大小的数组,然后使用普通的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];
nope,大小是动态的,我只需要知道如何在NSMutableArray中的testList [i] [n] – 2012-07-17 10:32:45
中添加对象,您不必首先声明大小,只需添加另一个对象即可。 C++中的列表比数组更多。 – Kuba 2012-07-17 10:34:36
[[testList objectAtIndex:i] insertObject:item atIndex:j];
要在@ 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'中添加更多的'NSMutableArray'(或'NSArray')对象,因为**在objective-c中没有多维数组**;你可以在KVC中使用'NSMutableDictionary'(或'NSDictionary');或者你可以将它们相互结合;或者你可以使用'NSMutableSet'(或'NSSet'),或者你可以将所有东西与所有东西结合起来......一切都是无限的,它取决于你想表示什么样的数据。 – holex 2012-07-17 16:10:23