从上一个视图获取数据时,如何在相同 class 中分配相互使用的变量

How to assign variables in same class that use each other when getting data from previous view

我在加载这个之前有一个视图控制器,它从我的数据库中获取不同的视频并下载它们 url,然后我将每个 url 传递给下一个 viewcontroller它是自己的字符串,使用 prepare(segue:...),在这个 class 中,我以编程方式使用我的自定义数据制作了一个滚动视图,但是我收到错误 Cannot use instance member 'itemOne' within property initializer; property initializers run before 'self' is available。我了解此错误的含义并且我尝试了惰性变量但我认为考虑到以前的视图控制器需要该变量将 url 数据发送到,如果这有意义的话,它不会起作用。那么,当您需要将数据发送到该变量时,您将如何处理,这是我的代码:

class HomeViewController: UIViewController {

// Variables use with the previous view controller to send data between them

var itemOne: String? 
var itemTwo: String?
var itemThree: String?
var itemFour: String?

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(collectionView)
    collectionView.backgroundColor = .white
    collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
    collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
    collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
    collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.5).isActive = true 

    collectionView.delegate = self
    collectionView.dataSource = self

}

fileprivate let data = [
    CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: itemOne!), // ERROR
    CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: itemTwo!), // ERROR
    CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: itemThree!) // ERROR
]
/....

您可以使数据成为计算结果属性

var data: [CustomData] {
    return [
        CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: itemOne!),
        CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: itemTwo!),
        CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: itemThree!)
    ]
}