I have a view Controller before this one is loaded that gets different videos from my database and gets their download url, then I pass each url to this next viewcontroller in it's own String using prepare(segue:...)
, In this class I have programatically made a scrollview with my custom data, however I get the error Cannot use instance member 'itemOne' within property initializer; property initializers run before 'self' is available
. I understand what this error means and I've tried lazy variables but I don't think it would work considering the previous view controller needs that variable to send the url data to, if that makes sense. So how would you go about this when you need to send data to that variable, here's my code:
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
]
/....