我一直试图了解以下问题。由于我是Swift的新手,无论我编写什么代码,都可能毫无意义,因此无法解释。我明白。这些代码是从一些在线教程中复制而来的,我正在尝试使用Core Data将数据持久性纳入其中。这是我陷入困境的地方。
希望有人可以提供帮助。
enter code here
Core Data model attached to the following view controller :
entity : Video
attribute : image - String
attribute : title - String
import UIKit
import CoreData
class VideoListScreenVC: UIViewController {
@IBOutlet weak var tableView: UITableView!
var videos : [Video] = [ ]
override func viewDidLoad() {
super.viewDidLoad()
videos = createArray()
}
func createArray() -> [Video] {
var tempVideos : [Video] = []
if let context = (UIApplication.shared.delegate as AppDelegate)?.persistentContainer.viewContext{
let videoX = Video(context: context)
let v1 = videoX.image
let t1 = videoX.title
let video1 = [v1,t1]
let video2 = [v1,t1]
let video3 = [v1,t1]
let video4 = [v1,t1]
let video5 = [v1,t1]
tempVideos.append(video1) // error : Cannot convert value of type '[String?]' to expected argument type 'Video'
// tempVideos.append(video2)
// tempVideos.append(video3)
// tempVideos.append(video4)
// tempVideos.append(video5)
return tempVideos
}
}
}