xcode 在多维 NSMutableArray 中添加对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11520415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding objects in Multidimensional NSMutableArray
提问by Rowie Po
I'm quite confused on how to add objects in multidimensional arrays.
我对如何在多维数组中添加对象感到很困惑。
Is initializing multidimensional arrays are the same with just a simple array?
初始化多维数组是否与仅一个简单数组相同?
This is my initialization.
这是我的初始化。
testList = [[NSMutableArray alloc] init];
I need to do something like
我需要做类似的事情
testList[i][j] = item;
i tried
我试过
[[[testList objectAtIndex:i]objectAtIndex:j] addObject:item];
but it doesn't seem to work :(
但它似乎不起作用:(
回答by Jakub
You are add to much C to do this. This is important to know how to NSMutableArray works and how it's different compare to 2D arrays known from C/C++.
你需要添加很多 C 来做到这一点。了解 NSMutableArray 的工作原理以及它与 C/C++ 已知的二维数组相比有何不同,这一点很重要。
In mutable array you could store another arrays. For example:
在可变数组中,您可以存储另一个数组。例如:
NSMutableArray *first = [[NSMutableArray alloc] init];
NSMutableArray *second = [[NSMutableArray alloc] init];
[first addObject:second];
Now you have array in first row in first array! This is something very like C/C++ 2D arrays.
现在您在第一个数组的第一行中有数组!这很像 C/C++ 2D 数组。
So if you want to add some object to "0,0"you do this:
因此,如果您想向“0,0”添加一些对象,请执行以下操作:
NSString *mytest = [[NSString alloc] initWithString:@"test"];
[second addObject:mytest];
[first addObject:second];
So now your secondcontains NSStringsand firstcontains second. Now you can loop this like you want.
所以现在你的第二个包含NSStrings,第一个包含second。现在您可以随意循环播放。
----EDIT:IF you want 1,0 you simply need another instance of secondNSMutableArray. For example you have this array:
----编辑:如果你想要 1,0 你只需要第二个NSMutableArray 的另一个实例。例如你有这个数组:
So here you will be have 3 elements in secondarray.
所以在这里你将在第二个数组中有 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];
}
You may want to implement NSCopying protocolto do this.
你可能想要实现NSCopying 协议来做到这一点。
回答by onegray
If you need a fixed size array, then use plain C array. Before using dynamic ObjC array it needs to create it:
如果您需要固定大小的数组,请使用普通的 C 数组。在使用动态 ObjC 数组之前,它需要创建它:
NSMutableArray* array = [NSMutableArray arrayWithCapacity:N];
for(int i=0; i<N; i++) {
[array addObject:[NSMutableArray arrayWithCapacity:M]];
}
UPD: The following methods might be helpful to work with such array:
UPD:以下方法可能有助于处理此类数组:
[[array objectAtIndex:i] addObject:obj];
[[array objectAtIndex:i] insertObject:obj atIndex:j];
[[array objectAtIndex:i] replaceObjectAtIndex:j withObject:obj];
回答by Girish Kolari
[[testList objectAtIndex:i] insertObject:item atIndex:j];
回答by Peter DeWeese
To expand upon @onegray's answer, you can set up some category methods to make soft multidimensional arrays easier to deal with.
为了扩展@onegray 的答案,您可以设置一些类别方法来使软多维数组更易于处理。
MultiMutableArray.h
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
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
example, MultiArrayTests.m
例如,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
I have written further details at http://peterdeweese.tumblr.com/post/27411932460/soft-multi-dimensional-arrays-in-objective-c
我在http://peterdeweese.tumblr.com/post/27411932460/soft-multi-dimensional-arrays-in-objective-c写了更多细节