removePersistentStore(:)不';不能用Swift返回布尔

removePersistentStore(:)不';不能用Swift返回布尔,swift,core-data,Swift,Core Data,我试图存储removePersistentStore(:)的返回值,根据文档,它显然是一个Bool: 我是这样使用它的: let removed = try! storeCoordinator.removePersistentStore(store) 但得到一个警报,即已删除只是无效: 这是怎么回事?我错过什么了吗 返回值 是如果删除存储,否则否 在NSPersistentStoreCoordinator文档中,指的是 方法: - (BOOL)removePersistentStore:(

我试图存储
removePersistentStore(:)
的返回值,根据文档,它显然是一个
Bool

我是这样使用它的:

let removed = try! storeCoordinator.removePersistentStore(store)
但得到一个警报,即
已删除
只是
无效

这是怎么回事?我错过什么了吗

返回值

如果删除存储,否则

NSPersistentStoreCoordinator
文档中,指的是 方法:

- (BOOL)removePersistentStore:(NSPersistentStore *)store
                    error:(NSError * _Nullable *)error
在Swift中,该方法抛出错误,而不是返回布尔值:

func removePersistentStore(_ store: NSPersistentStore) throws
这样你就可以称之为

try! storeCoordinator.removePersistentStore(persistentStore)
或者更好:

do { 
    try storeCoordinator.removePersistentStore(persistentStore)
} catch let error as NSError {
    print("failed", error.localizedDescription)
}
在“使用Swift与Cocoa和Objective-C”参考中进行比较:

如果Objective-C方法的最后一个非块参数的类型为NSError**,Swift将其替换为throws关键字,以指示该方法可能抛出错误。

如果生成Objective-C方法的错误返回BOOL值以指示方法调用的成功或失败,Swift会将函数的返回类型更改为Void

返回值

如果删除存储,否则

NSPersistentStoreCoordinator
文档中,指的是 方法:

- (BOOL)removePersistentStore:(NSPersistentStore *)store
                    error:(NSError * _Nullable *)error
在Swift中,该方法抛出错误,而不是返回布尔值:

func removePersistentStore(_ store: NSPersistentStore) throws
这样你就可以称之为

try! storeCoordinator.removePersistentStore(persistentStore)
或者更好:

do { 
    try storeCoordinator.removePersistentStore(persistentStore)
} catch let error as NSError {
    print("failed", error.localizedDescription)
}
在“使用Swift与Cocoa和Objective-C”参考中进行比较:

如果Objective-C方法的最后一个非块参数的类型为NSError**,Swift将其替换为throws关键字,以指示该方法可能抛出错误。

如果生成Objective-C方法的错误返回BOOL值以指示方法调用的成功或失败,Swift会将函数的返回类型更改为Void