ios - How to take Core Data and Filter it with NSPredicate Swift -
i learn , creating basic decision app. basics of app take user input category , things want fill category with.
now wanting display results on table view works click on each individual category used , able see things placed under ever category. getting being save core data trying use nspredicate filter out need. when run app there nothing in table view.
mainname have passed in different view controller capture , set name of category filter data. trying use in predicate filter.
i don't know if doing right great. independent study project doing finish degree , know self taught far. if have wrong please tell me. 1 of hundreds of different ways have tried right.
@iboutlet weak var tableview: uitableview! func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return whats.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell")! uitableviewcell let doing = whats[indexpath.row] cell.textlabel!.text = doing.valueforkey("what") as? string return cell } func loaddata(){ let appdelegate = uiapplication.sharedapplication().delegate as? appdelegate if let context = appdelegate?.managedobjectcontext{ let fetchrequest = nsfetchrequest(entityname: "doing") let namepredicate = nspredicate(format: "namesr.noun = '\(mainname)'") (whats nsarray).filteredarrayusingpredicate(namepredicate) fetchrequest.predicate = namepredicate { let results = try context.executefetchrequest(fetchrequest) whats = results as! [nsmanagedobject] } catch let error nserror { print("could not fetch \(error), \(error.userinfo)") } } } override func viewwillappear(animated: bool) { super.viewwillappear(animated) } override func viewdidload() { super.viewdidload() loaddata() }
if want use core data
in tableview
based app? can use nsfetchedresultscontroller
class , delegate
protocol methods. class specially designed tableview
!
Comments
Post a Comment