繁体   English   中英

为什么在 swift4 中有一个数组为空和错误?

[英]Why have a Array Empty and Error in swift4?

我花了几个小时试图在我的胶卷中找到一个工作。 我需要打开我的收藏视图项目的电影视图。 我可以遵循不同的指南和帖子,但它总是给我一个空数组。 我是新手,很抱歉我的问题,但我需要你的帮助。

这是我的代码:

var taskArrayScheda : NewFilm?

override func viewDidLoad() {
super.viewDidLoad()

self.fetchData()

if(self.taskArrayScheda != nil) {

let schedaOk = taskArrayScheda
mostraDatiNellaScheda(schedaOk!)

} else { print("errore array vuoto") }


}

func mostraDatiNellaScheda(_ sched:NewFilm) {

// get title
titoloScheda.text = sched.titolo
}

func fetchData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext


let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "NewFilm")

do {
taskArrayScheda = try context.fetch(NewAnime.fetchRequest())
💥ERROR ::::: Cannot assign value of type '[Any]' to type 'NewFilm?'

} catch {
print(error)
}
}    

fetch()返回一个数组。 但是目前您将fetch()结果分配给单个对象var taskArrayScheda

你需要这样的东西:

var taskArrayScheda: [NewFilm]?

那么你应该这样做:

taskArrayScheda = try context.fetch(NewAnime.fetchRequest()) as? [NewFilm]

我在这里假设NewAnimeNewAnime的子类, NewFilm两个类名来看似乎是有道理的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM