从Swift中的核心数据中检索随机字符串

时间:2014-11-27 07:11:14

标签: ios core-data swift nsfetchrequest

我已经搜索过这个答案,但却空手而归。出于某种原因,我在如何从Core Data String属性中检索随机条目时画了一个空白。我还有其他一切工作 - 创建,更新,删除......但似乎无法从堆栈中获取随机条目。

对此感到困惑。这是我的表视图控制器中的代码,它主要管理核心数据。

import UIKit
import CoreData

class TaskManagerViewController: UITableViewController, NSFetchedResultsControllerDelegate {

    let managedObjectContext = (UIApplication.sharedApplication().delegate as 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 as UITableViewCell
            let indexPath = tableView.indexPathForCell(cell)
            let taskController:TaskDetailViewController = segue.destinationViewController as TaskDetailViewController
            let task:Tasks = fetchedResultController.objectAtIndexPath(indexPath!) as 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 any resources that can be 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) as UITableViewCell
            let task = fetchedResultController.objectAtIndexPath(indexPath) as 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) as NSManagedObject
        managedObjectContext?.deleteObject(managedObject)
        managedObjectContext?.save(nil)
    }

    func controllerDidChangeContent(controller: NSFetchedResultsController!) {
        tableView.reloadData()
    }
}

2 个答案:

答案 0 :(得分:2)

这是最终提供我正在寻找的结果的代码:

(我在堆栈溢出时是新的,所以不知道如何在这里选择正确的答案。)

func randomTask() -> Tasks {

        let count = UInt32(fetchedResultController.fetchedObjects!.count)
        let index = Int(arc4random_uniform(count))
        var results = fetchedResultController.fetchedObjects![index] as Tasks
        answerLabel.text = results.valueForKey("desc") as NSString
        return results
    }

答案 1 :(得分:1)

func randomTask() -> Tasks {

   let count=UInt32(fetchedResultController.fetchedObjects!.count) 
   let index=Int(arc4random_uniform(count))
   return fetchedResultsController.fetchedObjects[index] as Tasks
}