Swift3 error : Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject'
Swift3 error : Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject'
所以,我正在尝试将我的待办事项列表应用程序更新为 swift 3 / iOS 10,但我在更新时遇到了这个错误。我查阅了互联网和 Whosebug,发现其他人也遇到了这个问题,但那里的解决方案对我没有帮助。
这只是一个文件,负责插入核心数据。
这是该文件的代码:
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)
}
}
}
所以它在第 18 行给出了一个错误,它是这样的:
Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject'
谁能帮帮我?
谢谢:)
请像这样更新第 18 行。我希望这对你有用。
for res:AnyObject in tempTasks as [AnyObject]
所以,我正在尝试将我的待办事项列表应用程序更新为 swift 3 / iOS 10,但我在更新时遇到了这个错误。我查阅了互联网和 Whosebug,发现其他人也遇到了这个问题,但那里的解决方案对我没有帮助。 这只是一个文件,负责插入核心数据。
这是该文件的代码:
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)
}
}
}
所以它在第 18 行给出了一个错误,它是这样的:
Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject'
谁能帮帮我? 谢谢:)
请像这样更新第 18 行。我希望这对你有用。
for res:AnyObject in tempTasks as [AnyObject]