json - 在 Swift 中将 JSON 字典导入到 Core Data

标签 json core-data import xcode6 xcode6.1

我在使用 Swift 将 bools 从 JSON 字典导入到下面的 Exercises 对象中时遇到了一些麻烦。 StretchEquipment 对象非常简单。我知道这非常简单,但我找不到可以建模的示例。这是我的数据模型: enter image description here

Stretch 对象是我的主要实体。 Equipment 对象将包含字符串。 Exercises 对象属性是 bool 值。

    let jsonURL = NSBundle.mainBundle().URLForResource("seed", withExtension: "json")!
    let jsonData = NSData(contentsOfURL: jsonURL)

    var error: NSError?
    let jsonArray = NSJSONSerialization.JSONObjectWithData(jsonData!, options: nil, error: &error) as NSArray

    // Inserts attributes for database

    let entity = NSEntityDescription.entityForName("Stretch", inManagedObjectContext: coreDataStack.context)!

    var counter = 0

    for jsonDictionary in jsonArray {

        counter++
        // listing of columns in JSON seed file
        let stretchDescription = jsonDictionary.valueForKey("stretchDescription") as String
        let generalArea = jsonDictionary.valueForKey("generalArea") as String
        let muscleGroup = jsonDictionary.valueForKey("muscleGroup") as String
        let equipmentCode = jsonDictionary.valueForKey("equipmentCode") as String
        let sideMultiplier = jsonDictionary.valueForKey("sideMultiplier") as NSNumber
        let advanced = jsonDictionary.valueForKey("advanced") as Bool
        let photo = jsonDictionary.valueForKey("photo") as NSData
        let runningOrWalking = jsonDictionary.valueForKey("runningOrWalking") as Bool
        let cycling = jsonDictionary.valueForKey("cycling") as Bool
        let weightLifting = jsonDictionary.valueForKey("weightLifting") as Bool
        let aerobics = jsonDictionary.valueForKey("aerobics") as Bool
        let raquetSports = jsonDictionary.valueForKey("raquestSports") as Bool
        let golf = jsonDictionary.valueForKey("golf") as Bool
        let yoga = jsonDictionary.valueForKey("yoga") as Bool
        let equipmentDescription = jsonDictionary.valueForKey("equipmentDescription") as String

        let stretch = Stretch(entity: entity, insertIntoManagedObjectContext:coreDataStack.context)

        stretch.stretchDescription = stretchDescription
        stretch.generalArea = generalArea
        stretch.muscleGroup = muscleGroup
        stretch.equipmentCode = equipmentCode
        stretch.sideMultiplier = sideMultiplier
        stretch.advanced = advanced
        stretch.photo = photo

        // Insert Equipment object
        let equipmentEntity = NSEntityDescription.entityForName("Equipment", inManagedObjectContext: coreDataStack.context)
        let equipmentDescriptionObject = Equipment(entity: equipmentEntity!, insertIntoManagedObjectContext: coreDataStack.context)

        stretch.equipmentUsed = equipmentDescriptionObject

        // Figure out exercises object
        let exercise = NSEntityDescription.entityForName("Exercises", inManagedObjectContext: coreDataStack.context)
        let exerciseObject = Exercises(entity:exercise!, insertIntoManagedObjectContext: coreDataStack.context)

这就是我被困住的地方。我将如何完成添加练习对象的属性?

最佳答案

我正在查看这个库(同步),它看起来很棒,可以将 JSON 对象转换为核心数据对象: https://github.com/hyperoslo/Sync

关于json - 在 Swift 中将 JSON 字典导入到 Core Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26853741/

相关文章:

javascript - json_encode 无法从 php 数组转换为 js 数组

iphone - 检测轻量级核心数据迁移

java - 将 Maven 项目 "YCSB"导入 Eclipse

xcode - 在 iOS Cocoa 静态库项目中使用 Objective-C 代码,在 iOS 应用程序项目中使用 Swift 代码

javascript - JSON 损坏的属性

PHP utf8/unicode 编码

ios - 数据库迁移后保存核心数据时Cocoa错误134030

javascript - 如何配置 TypeScript 以针对不正确的导入语句抛出错误/警告?

php - 这是什么类型的代码?

iphone - 使用核心数据和 ARC 进行简单的内存管理