在 Swift 中将 JSON 字典导入核心数据
Posted
技术标签:
【中文标题】在 Swift 中将 JSON 字典导入核心数据【英文标题】:Importing JSON dictionary to Core Data in Swift 【发布时间】:2014-11-10 21:57:20 【问题描述】:我在使用 Swift 将 JSON 字典中的布尔值导入到下面的 Exercises
对象时遇到了一些麻烦。 Stretch
和 Equipment
对象非常简单。我知道这很容易,但是我找不到可以建模的示例。这是我的数据模型:
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)
这就是我卡住的地方。我将如何完成为练习对象添加属性?
【问题讨论】:
【参考方案1】:我正在检查这个库(同步),它看起来很棒,可以将 JSON 对象转换为核心数据对象: https://github.com/hyperoslo/Sync
【讨论】:
【参考方案2】:这就是我卡住的地方。我将如何完成为练习对象添加属性?
与您向Stretch
对象添加属性的方式相同。您可以通过点符号轻松访问和设置Exercise
属性。
let exercise = NSEntityDescription.entityForName("Exercises", inManagedObjectContext: coreDataStack.context)
let exerciseObject = Exercises(entity:exercise!, insertIntoManagedObjectContext: coreDataStack.context)
exerciseObject.aerobics = aerobics
【讨论】:
谢谢!在教程中遵循是一回事,但有时非常简单的事情比我参与时更难;)以上是关于在 Swift 中将 JSON 字典导入核心数据的主要内容,如果未能解决你的问题,请参考以下文章