Ios Swift 2.0迁移错误

Ios Swift 2.0迁移错误,ios,swift,swift2,Ios,Swift,Swift2,我观看了WWDC会议,阅读了关于Swift的最新程序员书籍,并阅读了所有关于堆栈溢出的相关问题。从Swift 1.2迁移到Swift 2.0后,我修复了应用程序中的大多数错误 然而,仍有一些问题我没有解决 向下投射任何对象 错误: 无法从“[AnyObject]”向下转换为更可选的类型“[NSManagedObject]” 代码: 显示的错误发生在let fetchedResults=try…行中 我遇到的另一个奇怪错误是在我的AppDelegate中: 错误: “NSMutableDictio

我观看了WWDC会议,阅读了关于Swift的最新程序员书籍,并阅读了所有关于堆栈溢出的相关问题。从Swift 1.2迁移到Swift 2.0后,我修复了应用程序中的大多数错误

然而,仍有一些问题我没有解决

向下投射任何对象

错误:

无法从“[AnyObject]”向下转换为更可选的类型“[NSManagedObject]”

代码:

显示的错误发生在
let fetchedResults=try…
行中

我遇到的另一个奇怪错误是在我的AppDelegate中:

错误:

“NSMutableDictionary”不能转换为“[NSObject:AnyObject]”

代码:

我从未接触过上面的代码。所以我不知道为什么苹果的迁移工具没有正确地迁移它

我的AppDelegate中的另一个错误:

二进制运算符“&&”不能应用于两个布尔操作数

调用可以抛出,但未标记为“try”,并且未处理错误

代码:

同样,我没有触及AppDelegate的这一部分,也不确定上面的代码到底出了什么问题

无法从“[AnyObject]”向下转换为更可选的类型“[NSManagedObject]”

在Swift 1.2中,
executeFetchRequest(:)
返回了
[AnyObject]?
。在Swift 2中,它返回
[AnyObject]
,因为新的
try
…语法返回非可选的

(如果该方法返回
nil
,则该方法根本不会返回,控件将移动到
catch
块。)


“NSMutableDictionary”不能转换为“[NSObject:AnyObject]”

这意味着您试图将无法转换为Objective-C对象的内容插入到
NSMutableDictionary
。在您的情况下,我认为这是因为
error
是符合
ErrorType
的结构,而不是
NSError
对象。尝试添加
error1


调用可以抛出,但未标记为“try”,并且未处理错误


save()
可能引发错误,因此需要使用
try
执行,而不是作为
bool
进行计算。正如Martin R.在评论中指出的那样,的答案提供了一个完整的解决方案,因此我在此不再重复。

AppDelegate上的CoreData似乎存在严重的Swift 2迁移问题。通过将AppDelegate CoreData Swift 1.2完全替换为Swift 2.0,我解决了这个问题

您需要做的是删除以下内容

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {

lazy var managedObjectContext: NSManagedObjectContext = {

// MARK: - Core Data Saving support

func saveContext ()
并粘贴Swift 2.0代码:

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    // The persistent store coordinator for the application. This implementation creates and returns 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
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
    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 {
        // 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 as NSError
        let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // 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 \(wrappedError), \(wrappedError.userInfo)")
        abort()
    }

    return coordinator
}()

lazy var managedObjectContext: NSManagedObjectContext = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

// MARK: - Core Data Saving support

func saveContext () {
    if managedObjectContext.hasChanges {
        do {
            try managedObjectContext.save()
        } catch {
            // 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.
            let nserror = error as NSError
            NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
            abort()
         }
     }
 }
}

这应该可以解决问题。

对于#1,
try
在成功时为您打开可选项,因此只需删除问号。对于#2,
save()
可能会抛出错误,因此它需要使用
try
执行,而不是作为bool进行评估。如果moc.hasChanges&,您的问题是关于
!moc.save()
的答案如下:。-您的第一个问题或多或少是相同的,因为
managedContext.executeFetchRequest()
不再返回可选的。有关要解决的示例代码,请参阅Martin的链接。@MarkL当然,我会写一些东西。不知道你为什么会落选。这似乎是一个写得很好的问题,只有核心数据问题得到了回答。如果这个问题不起作用,请尝试使用CoreData创建一个新的Xcode项目。然后将应用程序委托中的CoreData代码替换为新项目中的代码。如果这能解决问题,请告诉我。谢谢我的故障-解决方案很好!只需要将SingleViewCoreData.sqlite更改为我的sqlite文件名Great。。很高兴它给了你答案。我也得到了完全相同的错误。问题是,只有当我在另一个文件中取消对完全不相关的
的注释时,才会出现错误。另外,我的项目从一开始就是Swift 2。。。
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()
        }
    }
}
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {

lazy var managedObjectContext: NSManagedObjectContext = {

// MARK: - Core Data Saving support

func saveContext ()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    // The persistent store coordinator for the application. This implementation creates and returns 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
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
    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 {
        // 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 as NSError
        let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // 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 \(wrappedError), \(wrappedError.userInfo)")
        abort()
    }

    return coordinator
}()

lazy var managedObjectContext: NSManagedObjectContext = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

// MARK: - Core Data Saving support

func saveContext () {
    if managedObjectContext.hasChanges {
        do {
            try managedObjectContext.save()
        } catch {
            // 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.
            let nserror = error as NSError
            NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
            abort()
         }
     }
 }
}