问题
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?
回答1:
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"];
回答2:
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.
来源:https://stackoverflow.com/questions/11361155/how-would-i-create-this-as3-data-structure-in-objective-c