输入'NSFastEnumerationIterator。元素'(又名'Any')不符合协议'AnyObject'

时间:2020-12-29 16:10:00

So, I'm trying to update my toDo list app to swift 3 / iOS 10, but I came across this error when updating. I have looked up the internet and * and found that someone else also sort of had this problem, but the solution there, didn't help for me. This is just one file, that takes care of inserting into Core Data.

所以,我想把我的toDo列表应用程序更新到swift 3 / ios10,但是我在更新时遇到了这个错误。我在网上查了一下*,发现有人也有这个问题,但是解决方法对我没有帮助。这只是一个文件,负责插入核心数据。

This is the code for that file :

这是该文件的代码:

import UIKit
import CoreData

var taskMgr: TaskManager = TaskManager()

struct task {
    var name = "not defined"
    var desc = "not defined"
}

class TaskManager: NSObject {
  var tasks = [task]()
  var persistenceHelper: PersistenceHelper = PersistenceHelper()

  override init(){

    let tempTasks:NSArray = persistenceHelper.list(entity: "Task")
    for res:AnyObject in tempTasks{
        tasks.append(task(name:res.valueForKey("name")as! String,desc:res.valueForKey("desc") as! String))
    }
  }


  func addTask(name:String, desc: String){

    var dicTask: Dictionary<String, String> = Dictionary<String,String>()
    dicTask["name"] = name
    dicTask["desc"] = desc

    if(persistenceHelper.save(entity: "Task", parameters: dicTask)){
        tasks.append(task(name: name, desc: desc))
    }
  }

  func removeTask(index:Int){
    let value:String = tasks[index].name 

    if(persistenceHelper.remove(entity: "Task", key: "name", value: value)){
        tasks.remove(at: index)
    }
  }

}

So it gives an error on line 18 which go's like this :

它在第18行给出了一个误差大概是这样的:

Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject'

“NSFastEnumerationIterator类型。元素'(又名'Any')不符合协议'AnyObject'

Can anyone help me? Thanks :)

谁能帮我吗?谢谢:)

1 个解决方案

#1


7  

Please Update row 18 like this. I hope this will work for you.

请像这样更新第18行。我希望这对你有用。

  for res:AnyObject in tempTasks as [AnyObject]

#1


7  

Please Update row 18 like this. I hope this will work for you.

请像这样更新第18行。我希望这对你有用。

  for res:AnyObject in tempTasks as [AnyObject]