ios - MVC model compliance -
i have app writes lot of data coredata db interrogate , give feedback based on user queries.
for have db actions in vc , move separate file called modeldb.
// mark: outlets @iboutlet weak var coursefield: uitextfield! @iboutlet weak var par3field: uitextfield! @iboutlet weak var par4field: uitextfield! @iboutlet weak var par5field: uitextfield! @iboutlet weak var errorlabel: uilabel! // mark: actions @ibaction func addnewcourse(sender: anyobject) { let appdel: appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let context: nsmanagedobjectcontext = appdel.managedobjectcontext let newcourse = nsentitydescription.insertnewobjectforentityforname("coursedb", inmanagedobjectcontext: context) newcourse.setvalue(coursefield.text, forkey: "coursename" newcourse.setvalue(int(par3field.text!), forkey: "totalpar3") newcourse.setvalue(int(par4field.text!), forkey: "totalpar4") newcourse.setvalue(int(par5field.text!), forkey: "totalpar5") { try context.save() performseguewithidentifier("back", sender: self) } catch { errorlabel.text = "data not saved" } }
from above can see i'm writing data db rather want in modeldb file , pass vc.
can please give me pointers on how achieve this?
new information & needed created global array capture , pass information contained in uitextfields:
newcourselist = [coursefield.text!, par3field.text!, par4field.text!, par5field.text!] modeldb.addnewcoursetodb(newcourselist)
when running fatal error "fatal error: unexpectedly found nil while unwrapping optional value", although array has 4 values.
the func pass to, inside modeldb class, looks this:
func addnewcoursetodb(_: [string]) { let appdel: appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let context: nsmanagedobjectcontext = appdel.managedobjectcontext // code }
but code not reach stage because of error. how fix passing of array?
in order communicate modeldb
class (assuming class), add property view controller. way establish relationship:
var modeldb: modeldb!
regarding managing communication modeldb
controller, there number of ways this, described in objc.io issue:
https://www.objc.io/issues/7-foundation/communication-patterns/
Comments
Post a Comment