Phil
Phil

Reputation: 3045

How would I create this AS3 data structure in objective c?

In AS3 if I wanted to quickly setup some data to work with, I would just do something like this...

static var playerData:Object = {position:{startX:200, startY:200}};

How would I create the same in Objective c?

Or..is there a better way to store a similar data structure in Obj-c?

Upvotes: 0

Views: 372

Answers (2)

tagyro
tagyro

Reputation: 1679

If it's something you'll use a lot maybe creating a custom class is a better way.

customClass <NSObject>

@property float startX
@property float startY

This way, you can use the dot just like in AS3:

customClassIstance.startX / startY

Let me know if you need further clarification.

Upvotes: 1

Fran Verona
Fran Verona

Reputation: 5476

To be clear, I'm not an expert in Objective-C, so maybe some Objective-C guru can help you better than I can. But I think that you can use NSMutableDictionary to simulate the Object AS3 behaviour:

id playerData = [[NSMutableDictionary alloc] init];
id position = [[NSMutableDictionary alloc] init];

[position setObject:@"200" forKey:@"startX"];
[position setObject:@"200" forKey:@"startY"];

[playerData setObject:position forKey:@"position"];

If in AS3 you access as:

playerData.position.startX

In Objective-C you can do something like this:

[[playerData objectForKey:@"position"] objectForKey:@"startX"];

Upvotes: 1

Related Questions