【问题标题】:Adding objects in Multidimensional NSMutableArray在多维 NSMutableArray 中添加对象
【发布时间】:2012-07-17 10:17:05
【问题描述】:

我对如何在多维数组中添加对象感到很困惑。

初始化多维数组和只是一个简单的数组一样吗?

这是我的初始化。

testList = [[NSMutableArray alloc] init];

我需要做类似的事情

testList[i][j] = item;

我试过了

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

但它似乎不起作用:(

【问题讨论】:

  • 你应该在你原来的testList中添加更多的NSMutableArray(或NSArray)对象,因为在objective-c中没有多维数组;您可以将NSMutableDictionary(或NSDictionary)与KVC 一起使用;或者您可以将它们相互结合;或者您可以使用NSMutableSet(或NSSet),或者您可以将所有内容与所有内容结合起来......一切都是无限的,这取决于您想要表示什么样的数据。

标签: objective-c xcode nsmutablearray


【解决方案1】:

你需要添加很多 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];

所以现在你的 second 包含 NSStrings 并且 first 包含 second现在您可以随意循环播放了。

----编辑: 如果你想要 1,0,你只需要 second NSMutableArray 的另一个实例。例如你有这个数组:

所以这里你将在 second 数组中有 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 处添加呢?
【解决方案2】:

如果你需要一个固定大小的数组,那么使用普通的 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];

【讨论】:

  • 不,尺寸是动态的,我只需要知道如何在 testList[i][n] 之类的东西中添加对象
  • 在 NSMutableArray 中你不必先声明大小,你只需添加另一个对象。在 c++ 中,列表比数组更多。
【解决方案3】:

为了扩展@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写了更多细节

【讨论】:

    【解决方案4】:
    [[testList objectAtIndex:i] insertObject:item atIndex:j];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 2013-02-09
      • 2012-01-25
      相关资源
      最近更新 更多