Swift 2.0迁移错误 [英] Swift 2.0 Migration errors
问题描述
我观看了WWDC会议,阅读了Swift上的新程序员,并阅读了Stack Overflow上我发现的所有相关问题。在从Swift 1.2迁移到Swift 2.0后,我修复了我的应用中的大多数错误。
I've watched the WWDC sessions, reading the new programmers book on Swift, and reading all the related questions on Stack Overflow I could find. I fixed most errors in my app after migrating from Swift 1.2 to Swift 2.0.
然而,还有一些我无法解决的问题。
However there's still a few that I've not managed to solve.
向下倾斜AnyObject
错误:
不能从'[AnyObject]'转发到更可选的类型'[NSManagedObject]'
Cannot downcast from '[AnyObject]' to a more optional type '[NSManagedObject]'
代码:
let fetchRequest = NSFetchRequest(entityName: formulaEntity)
var error: NSError?
do {
let fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]?
if let results = fetchedResults {
stocks = results
} else {
print("Could not fetch \(error), \(error!.userInfo)")
}
} catch {
print("ERROR: \(error)")
}
显示的错误发生在 let fetchedResults = try ...
line
The error shown is happening in the let fetchedResults = try...
line
我遇到的另一个奇怪错误是我的AppDelegate:
Another strange error I'm having is in my AppDelegate:
错误:
'NSMutableDictionary'不能转换为'[NSObject:AnyObject]'
'NSMutableDictionary' is not convertible to '[NSObject : AnyObject]'
代码:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Stocks.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
} catch var error1 as NSError {
error = error1
coordinator = nil
// Report any error we got.
let dict = NSMutableDictionary()
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 as [NSObject : AnyObject])
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
} catch {
fatalError()
}
return coordinator
}()
我还没有曾触及上面的代码。所以我不知道为什么苹果公司的迁移工具没有正确迁移它。
I have not ever touched the code above. So I have no idea why this wasn't migrated properly, by Apple's Migration tool.
我的AppDelegate中的另一个错误:
Another error in my AppDelegate:
二元运算符'&&'不能应用于两个Bool操作数
Binary operator '&&' cannot be applied to two Bool operands
调用可以抛出,但没有标记使用'try'并且不处理错误。
Call can throw, but it is not marked with 'try' and the error is not handled.
代码:
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save() {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
我再也没有触及AppDelegate的这一部分,也不确定上面的代码究竟出了什么问题。
Again I havn't touched this part of AppDelegate, and not sure what exactly is wrong with the code above.
推荐答案
无法从'[AnyObject]'向下转换为更可选的类型'[NSManagedObject]'
Cannot downcast from '[AnyObject]' to a more optional type '[NSManagedObject]'
在Swift 1.2中, executeFetchRequest(:_)
返回 [AnyObject]
。在Swift 2中,它返回 [AnyObject]
,因为新的 try
...语法返回一个非可选的。
In Swift 1.2, executeFetchRequest(:_)
returned [AnyObject]?
. In Swift 2, it returns [AnyObject]
because the new try
… syntax returns a non-optional.
(如果该方法将返回 nil
,则该方法根本不会返回,并且控制将移至 catch
block。)
(In the case that the method would return nil
, the method would not return at all, and control would move to the catch
block.)
'NSMutableDictionary'不能转换为'[NSObject:AnyObject]'
'NSMutableDictionary' is not convertible to '[NSObject : AnyObject]'
这意味着您正在尝试将某些内容插入 NSMutableDictionary
无法转换为Objective-C对象。在你的情况下,我认为这是因为错误
是一个符合 ErrorType
的结构,而不是 NSError
对象。请尝试添加 error1
。
This means you're trying to insert something into an NSMutableDictionary
that can't be converted to an Objective-C object. In your case, I think it's because error
is a struct conforming to ErrorType
, rather than an NSError
object. Try adding error1
instead.
呼叫可以抛出,但没有标记'尝试'并且没有处理错误。
Call can throw, but it is not marked with 'try' and the error is not handled.
save()
可能会抛出一个错误,所以它需要用 try
执行,而不是被评估为 bool
。正如Martin R.在评论中指出的那样,这个问题的答案提供了一个完整的解决方案,所以我不会重复它这里。
save()
might throw an error so it needs to be executed with try
, instead of being evaluated as a bool
. As Martin R. points out in the comments, the answer to this question provides a complete solution so I won't rehash it here.
这篇关于Swift 2.0迁移错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!