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总结
以上是内存溢出为你收集整理的在Swift中将JSON字典导入Core数据全部内容,希望文章能够帮你解决在Swift中将JSON字典导入Core数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)