将JSON字典导入Swift中的Core Data

4
我在使用Swift将JSON字典中的bool值导入Exercises对象时遇到了一些麻烦。导入StretchEquipment对象却很容易。我知道这是非常简单的事情,但我无法找到可以模仿的示例。以下是我的数据模型: enter image description here Stretch 对象是我的主要实体。 Equipment 对象将包含字符串。 Exercises 对象属性是布尔值。
    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)

我卡在这里了。我该如何完成添加Exercises对象属性的操作呢?

2个回答

2

1

我现在卡在这里了。我该如何完成添加Exercises对象属性的操作?

与添加Stretch对象属性的方式相同。您可以通过点符号轻松访问和设置Exercise属性。

let exercise = NSEntityDescription.entityForName("Exercises", inManagedObjectContext: coreDataStack.context)
let exerciseObject = Exercises(entity:exercise!, insertIntoManagedObjectContext: coreDataStack.context)
exerciseObject.aerobics = aerobics

谢谢!跟随教程是一回事,但有时候当我参与其中时,一些非常简单的事情比它们应该的更困难。 - iOSPadawan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接