微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Swift中将JSON字典导入Core数据

我在使用 Swift将JSON字典中的bool导入到下面的Exercises对象时遇到了一些麻烦.拉伸&设备对象非常简单.我知道这很简单,但我找不到一个可以模拟的例子.这是我的数据模型:

Stretch对象是我的主要实体.
Equipment对象将包含字符串.
练习对象属性是Bools.

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对象添加属性

解决方法

This is where I’m stuck. How would I finish up adding the attributes for the Exercises object?

与向Stretch对象添加属性的方式相同.您可以通过点表示法轻松访问和设置练习属性.

let exercise = NSEntityDescription.entityForName("Exercises",inManagedobjectContext: coreDataStack.context)
let exerciSEObject = Exercises(entity:exercise!,insertIntoManagedobjectContext: coreDataStack.context)
exerciSEObject.aerobics = aerobics

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐