ios - Delete items from tableview row and core data simultaneously -


i'm trying delete items tableview , entity called "books." have no idea if i'm remotely on right track, however. when try code:

func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {          var appdel:appdelegate = uiapplication.sharedapplication().delegate as! appdelegate         var context:nsmanagedobjectcontext = appdel.managedobjectcontext!         var request = nsfetchrequest(entityname: "books")          if editingstyle == uitableviewcelleditingstyle.delete {              addbook.mybooks.removeatindex(indexpath.row)             tableview.deleterowsatindexpaths([indexpath], withrowanimation: uitableviewrowanimation.automatic)             context.deleteobject(addbook.mybooks[indexpath.row] as! nsmanagedobject)          }     } 

i warning on last line says "cast 'string' unrelated type 'nsmanagedobject' fails." know how can around this? i've read can use fetchedresultscontroller handle core data in tables new programming , found method bit more confusing when setting core data in general. fetchedresultscontroller necessary manage data in tableview?

from error sounds addbook.mybooks array of strings.

the immediate problem deleteobject doesn't work on strings, works on managed objects-- is, instances of nsmanagedobject or subclass of nsmanagedobject. can't delete string core data that, have delete managed object corresponds string. error telling as! nsmanagedobject doesn't work on string, because string different kind of thing managed object.

[it's problem you're removing string @ indexpath.row via removeatindex, , later trying use string @ indexpath.row removed, that's not real problem here.]

what need find out managed object corresponds table view row you're deleting, , pass deleteobject. without fuller picture of how view controller works it's impossible how that, there couple of things clear:

  1. those first 3 lines in method not doing useful. cut them-- if made them work, they'd wrong approach here. don't want have fetch managed object you're deleting right here. time reach method should know enough delete it.

  2. it's not necessary use nsfetchedresultscontroller put core data table views. if you're new programming you'll find things lot easier if use it.


Comments