首页 文章

Xcode 6 Beta 4:使用未解析的标识符'NSFetchedResultsChangeInsert'

提问于
浏览
3

刚刚安装了Xcode 6 Beta 4,之前编译的代码现在在NSFetchedResultsChangeType的每个开关上都有“Unresolved Identifier”失败 . 我检查了发行说明,当然还要通过这里搜索,看看是否有其他人经历过这个,但到目前为止还没有任何结果 . 任何信息表示赞赏!

谢谢!

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

    println("Running CoreDataTVC.controllerDidChangeSection")

    switch type {


    case NSFetchedResultsChangeInsert:

        self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)

    case NSFetchedResultsChangeDelete:

        self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)

    default:

        return
    }
}

2 回答

  • 7

    当枚举时

    typedef NS_ENUM(NSUInteger, NSFetchedResultsChangeType) {
        NSFetchedResultsChangeInsert = 1,
        NSFetchedResultsChangeDelete = 2,
        NSFetchedResultsChangeMove = 3,
        NSFetchedResultsChangeUpdate = 4
    } ;
    

    映射到Swift,从枚举值中自动删除公共前缀:

    enum NSFetchedResultsChangeType : UInt {
        case Insert
        case Delete
        case Move
        case Update
    }
    

    比较"Using Swift with Cocoa and Objective-C"文档中的"Interacting with C APIs" .

    所以你的代码应该是这样的

    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
        switch type {
            case .Insert:
                self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
            case .Delete:
                self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
            default:
                return
        }
    }
    

    提示:如果您在Xcode中创建了一个“主 - 细节”应用程序并选择了“使用核心数据”,您将获得可以开始的示例代码 .

  • 1

    枚举NSFetchedResultsChangeType在NSFetchedResultsController内定义 .

    enum NSFetchedResultsChangeType : UInt {
        case Insert
        case Delete
        case Move
        case Update
    }
    

    要访问枚举值,您可以使用枚举类型,然后使用如下情况:

    switch type {  
    case NSFetchedResultsChangeType.Insert:  
        self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) 
    case NSFetchedResultsChangeType.Delete: 
        self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)  
    default:
        return
    }
    

    既然您知道类型类型是 NSFetchedResultsChangeType ,您也可以从开关案例中省略它,只需使用 case .Insert:case .Delete:

相关问题