ios - Retrieve Random String from Core Data in Swift -
i've searched answer have come empty handed. reason i'm drawing blank on how retrieve random entry core data string attribute. have else working - create, update, delete... can't seem handle on getting random entry stack.
feeling confused on this. here code table view controller manages core data.
import uikit import coredata class taskmanagerviewcontroller: uitableviewcontroller, nsfetchedresultscontrollerdelegate { let managedobjectcontext = (uiapplication.sharedapplication().delegate appdelegate).managedobjectcontext var fetchedresultcontroller: nsfetchedresultscontroller = nsfetchedresultscontroller() override func viewdidload() { super.viewdidload() fetchedresultcontroller = getfetchedresultcontroller() fetchedresultcontroller.delegate = self fetchedresultcontroller.performfetch(nil) } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject!) { if segue.identifier == "edit" { let cell = sender uitableviewcell let indexpath = tableview.indexpathforcell(cell) let taskcontroller:taskdetailviewcontroller = segue.destinationviewcontroller taskdetailviewcontroller let task:tasks = fetchedresultcontroller.objectatindexpath(indexpath!) tasks taskcontroller.task = task } } func getfetchedresultcontroller() -> nsfetchedresultscontroller { fetchedresultcontroller = nsfetchedresultscontroller(fetchrequest: taskfetchrequest(), managedobjectcontext: managedobjectcontext!, sectionnamekeypath: nil, cachename: nil) return fetchedresultcontroller } func taskfetchrequest() -> nsfetchrequest { let fetchrequest = nsfetchrequest(entityname: "tasks") let sortdescriptor = nssortdescriptor(key: "desc", ascending: true) fetchrequest.sortdescriptors = [sortdescriptor] return fetchrequest } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // #pragma mark - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { let numberofsections = fetchedresultcontroller.sections?.count return numberofsections! } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { let numberofrowsinsection = fetchedresultcontroller.sections?[section].numberofobjects return numberofrowsinsection! } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) uitableviewcell let task = fetchedresultcontroller.objectatindexpath(indexpath) tasks cell.textlabel.text = task.desc return cell } override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { let managedobject:nsmanagedobject = fetchedresultcontroller.objectatindexpath(indexpath) nsmanagedobject managedobjectcontext?.deleteobject(managedobject) managedobjectcontext?.save(nil) } func controllerdidchangecontent(controller: nsfetchedresultscontroller!) { tableview.reloaddata() } }
this code delivered results looking for:
(i'm new @ stack overflow not sure how select correct answer here.)
func randomtask() -> tasks { let count = uint32(fetchedresultcontroller.fetchedobjects!.count) let index = int(arc4random_uniform(count)) var results = fetchedresultcontroller.fetchedobjects![index] tasks answerlabel.text = results.valueforkey("desc") nsstring return results }
Comments
Post a Comment