0

我有 2 个动态创建的数组。我在网上阅读了一些例子,我无法完全理解。我看到的是,数组似乎是 1 维而不是 2。

下面的代码将一些对象分配给数组“combineObjectIssues”,然后将其添加到“combineAll”中以获得二维数组。我希望“currentObject.date”为索引 0,而“issuesDiscovered”数组为索引 1。

for (currentObject in currentObjects) {
        [combineObjectIssues addObject:currentObject.date]; //2D Array Row
        for (Issue *checkIssue in currentObject.issuesDiscovered) {
            if (checkIssue) {
                [issuesDiscovered addObject:checkIssue];
            }
        }
        [tempIssues addObject:[issuesDiscovered copy]]; // to combine all array of issues
        [combineOjectIssues addObjectsFromArray:[issuesDiscovered copy]]; //2D Array column
        [combineAll addObject:[combineObjectIssues copy]];
        [issuesDiscovered removeAllObjects]; //remove all objects;
        [combineObjectIssues removeAllObjects]; //remove all objects
    }
}

下面是 combineAll 数组的输出。

(
    (
    "2013-07-19 09:00:00",
    "<Issue: 0x8c171f0>",
    "<Issue: 0x8c16e50>",
    "<Issue: 0x8c16d30>",
    "<Issue: 0x8c16a10>",
    "<Issue: 0x8c16090>",
    "<Issue: 0x8c15bb0>",
    "<Issue: 0x8c156d0>"
),
    (
    "2013-07-13 14:30:00"
),
    (
    "2013-06-08 14:30:00",
    "<Issue: 0x8c10340>",
    "<Issue: 0x8c0fad0>",
    "<Issue: 0x8c0f590>",
    "<Issue: 0x8c0f0c0>"
),
    (
    "2013-05-04 11:30:00"
)
)

从输出中可以看出,它是一个我不想要的一维数组。我想要类似的东西,对于 [0][0],它包含日期,而对于 [0][1],它包含一系列问题。

我知道我的代码可能不正确。因此,请帮助我。感谢您的协助。

4

3 回答 3

2

尝试这个:

你总是可以得到这样的值,但语法可以不同。

[[combineAll objectAtIndex:0] objectAtIndex:1];

这样你就可以得到 [0][1] 的值。您可以这样做以使其成为二维数组。

你也可以这样做:

NSString* str = combineAll[0][1];

对于所有问题数据,您可以这样做:

NSArray* issueArray = combineAll[0];

这将返回 combineAll 数组位置 0 处的所有问题。

希望能帮助到你!!

于 2013-08-22T09:32:31.297 回答
1

存储 Cocoa 对象的多维数据结构问题经常出现。有时可以通过以更适合概念模型的方式重新考虑对象层次结构来解决它。但有时多维数组最好的机制。普通的 C 数组可以存储指向 Cocoa 对象的指针,并且可能是更好的处理方式。举例说明:

#import <Foundation/Foundation.h>

@interface Foo : NSObject
@property (nonatomic,strong) NSString *name;
@end

@implementation Foo
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        id p[10][10];
        //  create a bunch of foos
        for( uint8_t i = 0; i < 10; i++ ) {
            for( uint8_t j = 0; j < 10 ; j++ ) {
                Foo *aFoo = [[Foo alloc] init];
                aFoo.name = [NSString stringWithFormat:@"Foo_%02d_%02d",i,j];
                p[i][j] = aFoo;
            }
        }
        //  show that we can recover a Foo from C array
        Foo *someFoo = (Foo *)p[5][5];
        NSLog(@"Foo[5][5] = %@",someFoo.name);
        // prints Foo[5][5] = Foo_05_05 to the console
    }
    return 0;
}

或者,如果您想动态分配 C 数组,您将无需做更多的工作。看到这个要点

于 2013-08-22T09:56:01.100 回答
0

You're going to make life hard on yourself by using this kind of data structure. You'll always have to remember that the first index is a different type than the rest of them, and always having to do index shifting and so forth. But if you must, try something like this:

NSMutableArray *output = [NSMutableArray array];
for (YourClass *currentObject in currentObjects) {
    //Making assumption currentObject.issuesDiscovered in an NSArray! if it's not you'll need to initialize the second element differently
    NSArray *arrayForObject = @[currentObject.date,[currentObject.issuesDiscovered copy]];
    [output addObject:arrayForObject];
}
于 2013-08-26T07:45:59.630 回答