ios - How to compare fetched ObjectID with string? -
i have view controller shows details of item in core data. specific item passed controller previous controller.
here basic code:
class itemdetailviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource{ @iboutlet weak var itemlabel: uilabel! var selecteditemobjectid:string! override func viewdidappear(animated: bool) { //fetch the list of items context var appdel:appdelegate = (uiapplication.sharedapplication().delegate as! appdelegate) var request = nsfetchrequest(entityname: "item") var context:nsmanagedobjectcontext = appdel.managedobjectcontext! var error: nserror? var results:nsarray = context.executefetchrequest(request, error: &error)! //optional, avoids potential compiler error request.returnsobjectsasfaults = false if (results.count > 0) { fetchresult in results { println("populating details page...") println("fetchresult.objectid \(fetchresult.objectid)") println("selecteditemobjectid \(selecteditemobjectid)") //if array entry matches data passed us, populate item details screen if (fetchresult.objectid == selecteditemobjectid) { itemlabel.text = fetchresult.valueforkey("itemname") as? string } else { println("no items in database...") } } the problem having that, eyes, both of objectid's compare each other same. yet if statement not triggered , itemlabel.text never changed. here output when loop iterates through 1 should match:
populating details page... fetchresult.objectid 0xd000000000080000 <x-coredata://5f0ae77f-a83a-41bc-9361-0de37f120776/item/p2> selecteditemobjectid 0xd000000000080000 <x-coredata://5f0ae77f-a83a-41bc-9361-0de37f120776/item/p2> they same! if statement formed wrong? going using objectid's wrong way?
i figured out. in if comparison, needed use fetchresult.fetchresult.objectid.description instead of fetchresult.fetchresult.objectid.
see corrected code below:
class itemdetailviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource{ @iboutlet weak var itemlabel: uilabel! var selecteditemobjectid:string! override func viewdidappear(animated: bool) { //fetch the list of items context var appdel:appdelegate = (uiapplication.sharedapplication().delegate as! appdelegate) var request = nsfetchrequest(entityname: "item") var context:nsmanagedobjectcontext = appdel.managedobjectcontext! var error: nserror? var results:nsarray = context.executefetchrequest(request, error: &error)! //optional, avoids potential compiler error request.returnsobjectsasfaults = false if (results.count > 0) { fetchresult in results { println("populating details page...") println("fetchresult.objectid \(fetchresult.objectid)") println("selecteditemobjectid \(selecteditemobjectid)") //if array entry matches data passed us, populate item details screen if (fetchresult.objectid.description == selecteditemobjectid) { itemlabel.text = fetchresult.valueforkey("itemname") as? string } else { println("no items in database...") } }
Comments
Post a Comment