Objective-C converting to Swift 3.1 获取错误 'Any' is not convertible

Objective-C converting to Swift 3.1 getting error 'Any' is not convertible

我终于尝试将我的一个 Objective-C 应用程序转换为 Swift 3.1。我也在学习 Swift 的教程来帮助我。但是,当我尝试将 'for in' 循环转换为在 Obj-C 中成功运行的 Swift 时,我 运行 出现了以下错误。我在下面发布了 Swift 和 Objective-C 代码,并在 Swift 中注释了我收到错误的行。我收到错误 for managedObject: NSManagedObject in myResults{

错误状态

'Any' is not convertible to 'MSManageObject'

非常感谢为我指明正确方向的任何帮助。

//IN SWIFT 3.1
            let context: NSManagedObjectContext? = 
CoreDataHelper.shared().context
            let fetchRequest = NSFetchRequest<NSFetchRequestResult>()
            let entity = NSEntityDescription.entity(forEntityName: 
"Exhibitors", in: context!)
            fetchRequest.entity = entity
            var myResults : NSArray = try! 
CoreDataHelper.shared().context.fetch(fetchRequest) as NSArray
            self.objects = myResults as! [Any]
            if !(myResults != nil) || !((myResults.count) != nil) {
                print("No Exhibitor objects found to be deleted!")
            }
            else {
        //****Getting error 'Any' is not convertible to 'NSManagedObject'
                for managedObject: NSManagedObject in myResults {
                    if !(managedObject.value(forKey: "fav") == "Yes") {
                        context?.deleteObject(managedObject)
                        var error: Error? = nil
                        // Save the object to persistent store
                        if !context?.save(error) {
                            print("Can't Save! \(error) \
(error?.localizedDescription)")
                        }
                        print("Exhibitor object deleted!")
                    }
                }
            }


//IN OBJECTIVE-C
            NSManagedObjectContext *context = [[CoreDataHelper sharedHelper] 
context];

                    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] 
init];

                    NSEntityDescription *entity = [NSEntityDescription 
entityForName:@"Exhibitors" inManagedObjectContext:context];

                    [fetchRequest setEntity:entity];

                    NSArray *myResults = [[[CoreDataHelper sharedHelper] 
context] executeFetchRequest:fetchRequest error:nil];
                    self.objects = myResults;
                    if (!myResults || !myResults.count){
                        NSLog(@"No Exhibitor objects found to be deleted!");
                    }
                    else{
                        for (NSManagedObject *managedObject in myResults) {
                            if (![[managedObject valueForKey:@"fav"] 
isEqualToString:@"Yes"]) {


                                [context deleteObject:managedObject];


                                NSError *error = nil;
                                // Save the object to persistent store
                                if (![context save:&error]) {
                                    NSLog(@"Can't Save! %@ %@", error, 
[error localizedDescription]);
                                }
                                NSLog(@"Exhibitor object deleted!");

                            }
                        }
                    }

而不是这个:

var myResults : NSArray = try! CoreDataHelper.shared().context.fetch(fetchRequest) as NSArray

这样做:

let myResults : [NSManagedObject] = try! CoreDataHelper.shared().context.fetch(fetchRequest) 

您还应该少用 ! 运算符,因为它们可能会使您的应用程序崩溃,请尝试使用 guard let 模式或 ? 运算符。

主要问题是您注释的类型比编译器推断的更差。

不要那样做。不要注释类型,除非编译器告诉你这样做。

例如,托管对象上下文应该是非可选的

let context = CoreDataHelper.shared().context

您的提取 returns [NSManagedObject],从不使用 NSArray 并且从不将不同的类型转换为更未指定的类型,例如 [Any]

随便写

let fetchRequest = NSFetchRequest<NSFetchRequestResult>()
let entity = NSEntityDescription.entity(forEntityName: "Exhibitors", in: context)
fetchRequest.entity = entity
do {  // its always recommended to do a good error handling !!
   self.objects = try CoreDataHelper.shared().context.fetch(fetchRequest)
   for managedObject in self.objects { 

      ...

   }

} catch { print(error) }

如果 objects 为空,将跳过循环。