0

I like doing game development and I've been doing some with Sprite Kit, but I keep getting stuck while trying to make tree structures. Things for data and configurations.

In javascript I would just make objects of objects of objects and do whatever I wanted. So something like:

[{
    location: {x: 2, y:12},
    allowedNodes: [2,5],
    someflag: true
  }, ...
];

Or make objects and reference them everywhere. I know there is a NSMutableDictionary and structs do exist, but ARC won't let you put any references in them.

I also come from a C++ background and I feel I can do anything at all in C++, but I'm having a really tough time with Objective-C.

Sophie McCarrell
  • 2,831
  • 8
  • 31
  • 64

3 Answers3

1

NSDictionary is the easiest way to build a tree. It's used in a wide range of cases, for example JSON data always get represented with NSDictionary objects. You can put any reference into the dictionary.

Regarding your example of a tree, it could be represented on Objective-C this way:

@[
    @{
        @"location": @{
            @"x": @2,
            @"y": 12
        },
        @"allowedNodes": @[
            @2,
            @5
        ],
        @"someflag": @YES
    }
];
Igor Matyushkin
  • 778
  • 4
  • 4
  • Wow, considering the vagueness and angst in my question I'm surprised I got such a clear answer. The only thing I would add is how to access the values. Doing a search it sorta looked like you could do mydict[@0][@"location"][@"x"], but it does say it needs to be an objects, so @0 might not work. – Sophie McCarrell Mar 29 '14 at 20:54
  • `NSNumber *x = array[0][@"location"][@"x"];` – Igor Matyushkin Mar 30 '14 at 05:05
1

You're probably looking for the Objective-C 2.0 literals:

@{
    @"Key 1": @[@"Value 1", @"Value 2"],
    @"Key 2": @3,
    @"Key 3": @{@"Nested Key": @"Nested Value"}
}

These let you quickly create NSArray, NSDictionary, and NSNumber.

Community
  • 1
  • 1
smileyborg
  • 30,197
  • 11
  • 60
  • 73
0

Try using CFTree in Core Foundation:

https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFTreeRef/Reference/reference.html

Rich Tolley
  • 3,812
  • 1
  • 30
  • 39