如何在swift 2.2(XCode 7.3)中更改storeCoordinator.addPersistentStoreWithType
我有一个项目,并在我的项目中使用此代码:如何在swift 2.2(XCode 7.3)中更改storeCoordinator.addPersistentStoreWithType,swift,xcode,xcode7.3,Swift,Xcode,Xcode7.3,我有一个项目,并在我的项目中使用此代码: if storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error:&error ) != nil{ if error != nil{ print(error!.localizedDescription) abort() } } 但在s
if storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error:&error ) != nil{
if error != nil{
print(error!.localizedDescription)
abort()
}
}
但在swift 2.2中,此代码有错误…xcode显示此错误:
调用中的额外参数“error”
另一个代码是:
class func saveManagedObjectContext(managedObjectContext:NSManagedObjectContext)->Bool{
if managedObjectContext.save(nil){
return true
}else{
return false
}
}
在这些行中有一些错误:
调用可以抛出,但它没有标记为“try”,错误也没有
处理
及
传递给不带参数的调用的参数
您需要将对
addPersistentSore…
的调用包含在do/try/catch处理程序中:
do {
myNewStore = try storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
// other code on success ....
}
catch let error as NSError {
Swift.print(error!.localizedDescription)
abort()
}
您可以阅读Swifts抛出/捕获错误处理,例如:
另请参阅Apple文档(注意它是Swift 3.0.1)谢谢……但您的答案是错误的……因为“try”不能与“if”一起使用,我为您添加了如何获取结果的方法。在try/catch构造中不再需要if。没关系。thanls很多