ios - 类型 'NSPersistentStore' 在 swift 中不符合协议(protocol) 'BooleanType'

标签 ios swift

此代码显示我的错误所在的位置:

do {
    //error showing at this line         
    if try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL: url, options:nil) {
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }
} catch {
    print(error)
}

编译器给出此错误:Type 'NSPersistentStore' does not conform to protocol 'BooleanType'

为什么会出现此错误?

最佳答案

您正在使用的函数定义:

func addPersistentStoreWithType(_ storeType: String, configuration configuration: String?, URL storeURL: NSURL?, options options: [NSObject : AnyObject]?) throws -> NSPersistentStore

所以这是一个可以抛出并返回持久存储的函数。

您的代码说 if try coordinator!.addPers...,即“如果返回的持久存储为真,则...”。持久存储不是 true(或 false),因此它不是 bool 值。您编写的代码就像返回一个状态,但返回一个(非可选)对象(假设函数没有抛出)。

关于ios - 类型 'NSPersistentStore' 在 swift 中不符合协议(protocol) 'BooleanType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33832856/

相关文章:

ios - 为 swift 框架创建 Cocoapod

ios - 解码 H264 VideoToolkit API 失败,在 VTDecompressionSessionDecodeFrame 中出现错误 -12911

Swift:UITableViewCell 中的 UITextField 委托(delegate)不起作用

ios - 使用 Parse-Server 时,Parse LocalDataStore 不起作用 - 'Method requires Pinning enabled.'

ios - SCNAction playAudio似乎导致崩溃

swift - 快速创建饼图

swift - 使用 SWIFT 格式从字符串中获取值

swift - 如何使用 UIDatePicker 输入管理带有两个 UITextField 的 firstResponder

ios - 在旋转界面方向时将 contentOffset 保持在 UICollectionView 中

ios - Swift:使用 Web 服务的数据填充 collectionView