我试图将一些核心数据代码从swift 2迁移到swift4。代码如下:
/// Removes the existing model store specfied by the receiver.
///
/// - returns: A tuple value containing a boolean to indicate success and an error object if an error occurred.
public func removeExistingModelStore() -> (success: Bool, error: NSError?) {
var error: NSError?
let fileManager = FileManager.default
if let storePath = storeURL.path {
if fileManager.fileExists(atPath: storePath) {
let success: Bool
do {
try fileManager.removeItem(at: storeURL)
success = true
} catch let error1 as NSError {
error = error1
success = false
}
if !success {
print("*** \(String(describing: CoreDataModel.self)) ERROR: [\(#line)] \(#function) Could not remove model store at url: \(String(describing: error))")
}
return (success, error)
}
}
return (false, nil)
}
构建错误显示“用于条件绑定的初始化程序必须具有可选类型,而不是'String'”。所以我用谷歌搜索并遵循了删除可选绑定的建议:
let storePath = storeURL.path { ... }
然后在上述位置出现两个新错误:1.无法调用非函数类型'String'的值; 2.在其初始值内使用的变量
我相信上面的代码主要是用于设置/拆卸核心数据模型的样板代码。我是初学者,所以请帮忙!