使用 Hyperolso Sync 将多级 JSON 对象映射到 NSManagedObject

Map a multi-level JSON object to NSManagedObject with Hyperolso Sync

我正在使用 HyperOslo 的 Sync 从 API:

中获取一个 JSON 对象

JSON 对象:

{"user":
  {"name" : "damien",
   "email" : "myemail@gmail.com",
   "settings" : 
    { "notification" : "true",
      "secretKey" : "dzadd7z5a7dzd5azd"
    }
  }
}

这是我的用户对象,是 NSManagedObject 的子类。使用 属性 "settings" 作为自定义 NSObject:

class User: NSManagedObject {
    @NSManaged var name: String?
    @NSManaged var email: String?
    @NSManaged var settings: Settings?
}

这看起来不可能! (如果知道其他第三方框架?)

as the doc,解决方案是使用二进制数据类型:

...但是如何在我的模型中构建此 属性 的 getter? (我知道下面的 getter 有任何意义):

 @NSManaged var settings: Settings?{
  return [NSKeyedUnarchiver unarchiveObjectWithData:self.settings];
}

框架的构建者建议我 a good alternative solution:

Why not just create a new Core Data table for the new object?

1 - 在核心数据中添加实体

2 - 为实体创建 NSManagedObject 子类

class Setting: NSManagedObject {
    @NSManaged var notification: Bool?
    @NSManaged var notification: String?
}

3 - 在用户信息中设置hyper.remoteKey = secretKey

If your JSON says "secret_key" Sync will map it to secretKey in Core Data. But if your JSON says "secretKey" it doesn't know how to transform it to secretKey because it only does automatic transformations for snake_case. So if you wan to use secretKey from your JSON you need to tell Sync this. You do it by adding hyper.remoteKey (key) and secretKey as value in the user Info.

3 - 在 User 上创建 "setting" 关系(不要忘记在模型中添加 属性)