string - UITextField does not get the text value of an Array -


i using core data save user input in array. until code works , problem time element of array , put in uitextfield text. code has no errors , not work should.

@iboutlet weak var quotetextfield: uilabel! @iboutlet weak var addquote: uibutton! @iboutlet weak var deletequote: uibutton!  var bookarray: array<anyobject> = []  override func viewdidload() { super.viewdidload()          //get info on coredata var appdel: appdelegate = (uiapplication.sharedapplication().delegate as! appdelegate) var context:nsmanagedobjectcontext = appdel.managedobjectcontext! var request = nsfetchrequest(entityname: "bookarray") request.returnsobjectsasfaults = false bookarray = context.executefetchrequest(request, error: nil)!  if (bookarray.count != 0){     var randomindex = int(arc4random_uniform(uint32(bookarray.count)))     quotetextfield.text = bookarray[randomindex] as? string    } } 

datamodel

and saving user input, located in view controller:

 @ibaction func saveondata(sender: anyobject) {      var quoteinputtext = quoteinput.text      var appdel: appdelegate = (uiapplication.sharedapplication().delegate as! appdelegate)     var context:nsmanagedobjectcontext = appdel.managedobjectcontext!      var newquote = nsentitydescription.insertnewobjectforentityforname("bookarray", inmanagedobjectcontext: context) as! nsmanagedobject      newquote.setvalue(quoteinputtext, forkey: "myfirstbook")      context.save(nil)      println(newquote)  } 

going line line.

first create fetch request:

var appdel: appdelegate = (uiapplication.sharedapplication().delegate as! appdelegate) var context:nsmanagedobjectcontext = appdel.managedobjectcontext! var request = nsfetchrequest(entityname: "bookarray") request.returnsobjectsasfaults = false 

then assign result of fetch request array called bookarray:

bookarray = context.executefetchrequest(request, error: nil)! 

function executefetchrequest returns [anyobject]. in case return array of bookarray entities [bookarray].

so, when call:

bookarray[randomindex] 

you instance of bookarray. of course conditional downcast string fail ;)

here have do:

1) add following class project:

import foundation import coredata  @objc(bookarray) class bookarray:nsmanagedobject {     @nsmanaged var myfirstbook:string } 

then open core data model, click on bookarray entity , make sure et class bookarray in attributes inspector:

enter image description here

2) such implementation can rewrite save function to:

 @ibaction func saveondata(sender: anyobject) {      var quoteinputtext = quoteinput.text      var appdel: appdelegate = (uiapplication.sharedapplication().delegate as! appdelegate)     var context:nsmanagedobjectcontext = appdel.managedobjectcontext!      var newquote = nsentitydescription.insertnewobjectforentityforname("bookarray", inmanagedobjectcontext: context) as! bookarray      newquote.myfirstbook = quoteinputtext      context.save(nil)      println(newquote)  } 

3) change unwrapping part of viewdidload function to:

if (bookarray.count != 0){     var randomindex = int(arc4random_uniform(uint32(bookarray.count)))     let bookarrayinstance = bookarray[randomindex] as! bookarray     quotetextfield.text = bookarrayinstance.myfirstbook    } } 

Comments