在 Swift 核心数据项目中实现 UISearchController

Implementing a UISearchController in Swift Core Data Project

我目前正在参加 iOS 开发课程。作为作业的一部分,我的任务是使用 Swift.

中的核心数据在笔记跟踪项目中创建 UISearchController

我发现的每个示例都在 Objective-C 中或正在过滤静态数组。 2014 年 12 月更新的 Apple "sample" 代码无法在 Xcode 6.3.

中编译

要添加 UISearchController,我有 3 项主要任务要做:

1) 创建一个视图控制器来显示搜索结果。我正在使用 TableViewController。

2) 创建一个 UISearchController,并将它传递给我的搜索结果视图控制器。

我 "stumping" 现在要做的是获取 managedObjectsContext 中的对象。 在尝试添加 UISearchController 之前,我的应用程序运行正常美好的。我可以添加、编辑和删除项目。我在 Xcode 6.3 中使用 "canned" 核心数据代码,在 AppDelegate 中使用堆栈。

class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate

    var searchController: UISearchController? = nil

func addSearchBar() {
    var resultsController = SearchResultsTableViewController()
    resultsController.notes = // stumped what to call the notes. Trying to call an array from the Notes class below
    resultsController.delegate = self

    searchController = UISearchController(searchResultsController: resultsController)
    searchController!.searchResultsUpdater = resultsController
    searchController!.searchBar.frame = CGRect(
        x: searchController!.searchBar.frame.origin.x,
        y: searchController!.searchBar.frame.origin.y, width: searchController!.searchBar.frame.size.width, height: 44.0)

    tableView.tableHeaderView = searchController!.searchBar
    self.definesPresentationContext = true
}

3) 当搜索文本发生变化时,UISearchController 会通知它的searchResultsUpdater(一个符合UISearchResultsUpdating 的class)。我正在使用我的搜索结果视图控制器实现这个协议,这样我就可以更新过滤后的结果。

下面是我的Note: NSManagedObjectclass:

import Foundation
import CoreData

class Note: NSManagedObject {

@NSManaged var dateCreated: NSDate
@NSManaged var dateEdited: NSDate
@NSManaged var noteTitle: String
@NSManaged var noteBody: String

// TODO: adding this to make it easier to handle names
class func notes() -> NSArray {
    let whereMyNotesAreStored = // Need syntax for where my MyManagedObjectContext is located
    let dataArray = NSArray(contentsOfFile: whereMyNotesAreStored!)

    var notesArray = NSMutableArray()

    for dictionary in dataArray {
        var note = Note()
        note.noteTitle = dictionary["noteTitle"] as! String
        note.noteBody = dictionary["noteBody"] as! String
        note.dateCreated = dictionary["dateCreated"] as! String
        note.dateEdited = dictionary["dateEdited"] as! String

        notesArray.addObject(note)
    }

    return NSArray(array: notesArray as! [AnyObject])
}

}

有两种设置上下文的方法:

回调 App Delegate:,像这样

    let appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context = appDelegate.managedObjectContext!

将上下文从 App Delegate 转发 到 Master View Controller,然后主 View Controller 将其传递给任何后续的 View Controller,等等。每个 View Controller 都需要一个 属性 为上下文定义;当实例化一个新的VC时,在VC是presented/pushed之前设置上下文,例如:

    class CustomViewController : UIViewController {
        var managedObjectContext : NSManagedObjectContext
        ...

并且,在加载新的视图控制器时,

    let newVC = CustomViewController()
    newVC.managedObjectContext = self.managedObjectContext
    ...

要访问对象,请使用 NSFetchRequestNSFetchedResultsController 创建一个结果数组,然后您可以将其传递给 resultsController。例如。对于获取请求:

    let fetch = NSFetchRequest(entityName: "Notes")
    var error : NSError? = nil
    let fetchedResults = managedObjectContext?.executeFetchRequest(fetch, error: &error)

(您的 notes() 函数在错误的轨道上 - 您不会使用 NSArray(contentsOfFile:) 来访问 CoreData 对象。此外,您必须为 NSManagedObject 子类使用指定的初始化程序:所以不是 var note = Notes()但是 var note = Notes(entity: NSEntityDescription, insertIntoManagedObjectContext: NSManagedObjectContext?)