0

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
]
/....
Nathan
  • 1,393
  • 1
  • 9
  • 40
  • Does this answer your question? [How to initialize properties that depend on each other](https://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other) – Joakim Danielson Jan 06 '20 at 11:03
  • @JoakimDanielson no because in that question - or any other questions like this for that matter, it doesn't involve sending data between views which doesn't allow me to do what that answer says. – Nathan Jan 06 '20 at 11:05
  • I don't think that is a correct conclusion, did you try to use `lazy`? Anyway there are other similar solutions, like declaring the array and initialise it as empty and then add the data in `viewDidLoad` instead – Joakim Danielson Jan 06 '20 at 11:09
  • @JoakimDanielson I said it my question that I tried to use lazy and If i did that then the data wouldn't be operated – Nathan Jan 06 '20 at 11:10
  • Actually when I copy your code to a playground and declare the array as `lazy var data` I don't get any compilation error but maybe I don't understand the issue – Joakim Danielson Jan 06 '20 at 11:16
  • Well if the accepted answer works then surely using `lazy` must work too. – Joakim Danielson Jan 06 '20 at 11:21

1 Answers1

0

You can make data a computed property

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!)
    ]
}
tuyen le
  • 305
  • 5
  • 11
  • more on swift Properties for your convenient https://docs.swift.org/swift-book/LanguageGuide/Properties.html – tuyen le Jan 06 '20 at 11:15