Search code examples
jsoncore-dataimportxcode6xcode6.1

Importing JSON dictionary to Core Data in Swift


I've run into a bit of trouble importing bools from JSON dictionary into the Exercises object below using Swift. The Stretch & Equipment objects are a snap. I know it's something really easy, but I'm unable to find an example I can model. Here's my data model: enter image description here

The Stretch object is my main entity. The Equipment object will contain strings. The Exercises object attributes are 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)

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


Solution

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

    The same way you added attributes to Stretch object. You can easily access and set the Exercise attributes via dot notation.

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