什么是Objective-C等效于多维Javascript对象?

时间:2013-05-01 00:32:32

标签: javascript objective-c object

在Javascript中,您可以创建一个多维对象(见下文)。在Objective C中实现这一目标的正确方法是什么?

此对象代表游戏中的关卡。

var Levels = {
    Level1:{shapes:[{
  bodytype : "dynamic",
  h : "50.0000",
  nameid : "hofN7-1",
  props : {
    id : "properties"}
,
  rotation : "0.0000",
  type : "square",
  uid : "Av2EZQh",
  w : "50.0000",
  x : "20.0000",
  y : "20.0000"}
,
{
  bodytype : "dynamic",
  h : "50.0000",
  nameid : "hofN7-2",
  props : {
    gravMassScale : "2",
    id : "properties",
    inertia : "2",
    isBullet : true,
    torque : "2",
    velocity : {
      x : "2",
      y : "2"}
}...etc

1 个答案:

答案 0 :(得分:7)

这将是一个NSDictionary,NSStrings作为键,并为孩子们提供嵌套的NSDictionaries,NSStrings,NSArrays和NSNumbers的组合。

将代码机械翻译成Objective-C语法将是:

NSDictionary *levels = @{
    @"Level1" : @{ @"shapes" : @[ @{
  @"bodytype" : @"dynamic",
  @"h" : @"50.0000",
  @"nameid" : @"hofN7-1",
  @"props" : @{
    @"id" : @"properties"}
,
  @"rotation" : @"0.0000",
  @"type" : @"square",
  @"uid" : @"Av2EZQh",
  @"w" : @"50.0000",
  @"x" : @"20.0000",
  @"y" : @"20.0000"}
,
@{
  @"bodytype" : @"dynamic",
  @"h" : @"50.0000",
  @"nameid" : @"hofN7-2",
  @"props" : @{
    @"gravMassScale" : @"2",
    @"id" : @"properties",
    @"inertia" : @"2",
    @"isBullet" : @YES,
    @"torque" : @"2",
    @"velocity" : {
      @"x" : @"2",
      @"y" : @"2"}
} …