简体   繁体   English

如何创建二维数组目标C

[英]How to create a 2D Array Objective C

I have 2 arrays that were created dynamically. 我有2个动态创建的数组。 I have read some examples on the net, and I could not understand it fully. 我已经在网上阅读了一些示例,但我无法完全理解。 What I see, is that the arrays seems to be 1 Dimension and not 2. 我看到的是,这些数组似乎是1维而不是2维。

The codes below assigns some objects to array "combineObjectIssues", which will then be added into "combineAll" to get a 2D-Array. 下面的代码将一些对象分配给数组“ combineObjectIssues”,然后将其添加到“ combineAll”中以获得2D数组。 I want "currentObject.date" to be index 0, while "issuesDiscovered" array to be index 1. 我希望“ 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
    }
}

Below is my output for combineAll array. 以下是我的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"
)
)

As you can see from the output, its a 1 dimension array, which I do not want. 从输出中可以看到,它是一维数组,我不想要。 I want to have something like, for [0][0], it contains the date, whereby for [0][1], it contains an array of issues. 我想要类似[0] [0]的日期,而对于[0] [1]的日期则包含一系列问题。

I know my codes might not be right. 我知道我的代码可能不正确。 As such, kindly assist me. 因此,请协助我。 Your assistance(s) are appreciated. 感谢您的协助。

Try this: 尝试这个:

you can always get like that values but syntax can be different. 您总是可以得到类似的值,但是语法可以不同。

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

this way you can get the value for [0][1]. 这样,您可以获得[0] [1]的值。 you can do like this to make it 2 dimensional array. 您可以像这样使它成为二维数组。

also you can do this: 您也可以这样做:

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

For all the issue data you can do this: 对于所有问题数据,您可以执行以下操作:

NSArray* issueArray = combineAll[0];

this will return all the issues at position 0 of combineAll array. 这将返回CombineAll数组位置0处的所有问题。

Hope it Helps!! 希望能帮助到你!!

The question of multidimensional data structures to store Cocoa objects comes up frequently. 多维数据结构存储可可对象的问题经常出现。 Sometimes it can be solved by re-thinking your object hierarchy in a way that better fits the conceptual model. 有时可以通过以一种更适合概念模型的方式重新考虑对象层次结构来解决。 But sometimes a multidimensional array is the best mechanism. 但是有时候多维数组最好的机制。 Ordinary C arrays can store pointers to Cocoa objects, and may be a better way of dealing with this. 普通的C数组可以存储指向Cocoa对象的指针,并且可能是处理此问题的更好方法。 By way of illustration: 举例说明:

#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;
}

Or, if you want to allocate the C array dynamically, you'll have little more work to do. 或者,如果您想动态分配C数组,那么您将需要做更多的工作。 See this gist 看到这个要点

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM