swift - Array of structs: How to save in coredata? -
i´m trying save array of structs coredata. did lot of research, cannot find solution. here´s i´ve got:
import cocoa import coredata class viewcontroller: nsviewcontroller { struct studentsstruct { let firstname: string let lastname: string let age: int } let studentsdata: [studentsstruct] = [studentsstruct(firstname: "albert", lastname: "miller", age: 24), studentsstruct(firstname: "susan", lastname: "gordon", age: 24), studentsstruct(firstname: "henry", lastname: "colbert", age: 24)] override func viewdidload() { super.viewdidload() let student: student = nsentitydescription.insertnewobject(forentityname: "student", into: databasecontroller.getcontext()) as! student items in studentsdata { student.firstname = studentsstruct.firstname student.lastname = studentsstruct.lastname student.age = studentsstruct.age } databasecontroller.savecontext() let fetchrequest: nsfetchrequest<student> = student.fetchrequest() print (student) } } the databasecontroller solution i´ve got tutorial: https://www.youtube.com/watch?v=da6w7wdh0dw it´s not important, it´s making "getcontext" function. whats important, in teh commandline "student.firstname = studentsstruct.firstname" i´m getting error "instance member "firstname" cannot used on type viewcontroller.studentstruct. after trying , trying, i´m running out of ideas how array of structs coredata.
this databasecontroller file:
import foundation import coredata class databasecontroller { private init() { } class func getcontext() -> nsmanagedobjectcontext { return databasecontroller.persistentcontainer.viewcontext } // mark: - core data stack static var persistentcontainer: nspersistentcontainer = { let container = nspersistentcontainer(name: "studentcorefile") container.loadpersistentstores(completionhandler: { (storedescription, error) in if let error = error { fatalerror("unresolved error \(error)") } }) return container }() class func savecontext () { let context = databasecontroller.persistentcontainer.viewcontext if context.haschanges { { try context.save() } catch { // replace implementation code handle error appropriately. // fatalerror() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. let nserror = error nserror fatalerror("unresolved error \(nserror), \(nserror.userinfo)") } } } } for in advance!
ok, right, forgot execute fetchrequest. here´s current code:
import cocoa import coredata class viewcontroller: nsviewcontroller { struct studentsstruct { let firstname: string let lastname: string let age: int } let studentsdata: [studentsstruct] = [studentsstruct(firstname: "albert", lastname: "miller", age: 24), studentsstruct(firstname: "susan", lastname: "gordon", age: 24), studentsstruct(firstname: "henry", lastname: "colbert", age: 24)] override func viewdidload() { super.viewdidload() let student: student = nsentitydescription.insertnewobject(forentityname: "student", into: databasecontroller.getcontext()) as! student item in studentsdata { let student: student = nsentitydescription.insertnewobject(forentityname: "student", into: databasecontroller.getcontext()) as! student student.firstname = item.firstname student.lastname = item.lastname student.age = int16(item.age) } databasecontroller.savecontext() let fetchrequest: nsfetchrequest<student> = student.fetchrequest() { let searchresults = try databasecontroller.getcontext().fetch(fetchrequest) print("number of results: \(searchresults.count)") result in searchresults [student] { print(student) } } catch { print ("error") } } } it´s running without errors. i´m getting 32 search results. every entry is: age = 0; firstname = nil; lastname = nil;
for comparison, code, without loop working:
import cocoa import coredata class viewcontroller: nsviewcontroller { override func viewdidload() { super.viewdidload() let student: student = nsentitydescription.insertnewobject(forentityname: "student", into: databasecontroller.getcontext()) as! student student.firstname = "henry" student.lastname = "miller" student.age = 22 databasecontroller.savecontext() let fetchrequest: nsfetchrequest<student> = student.fetchrequest() { let searchresults = try databasecontroller.getcontext().fetch(fetchrequest) print("number of results: \(searchresults.count)") result in searchresults [student] { print(student) } } catch { print ("error") } } }
you need access item in loop accessing same object student object in loop instead of need create new student in every iteration of loop.
for item in studentsdata { //create new student in loop let student = nsentitydescription.insertnewobject(forentityname: "student", into: databasecontroller.getcontext()) as! student //to firstname, lastname , age access item student.firstname = item.firstname student.lastname = item.lastname student.age = item.age } //save context databasecontroller.savecontext()
Comments
Post a Comment