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

一曲冷凌霜 提交于 2019-12-13 07:25:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!