-1

Following Situation:

I have 2 Controllers, a ViewController and a CollectionViewController. The normal ViewController is supposed to collect data from the user and when the start button is clicked, an algorithm solves the problem and returns the result. The Result is supposed to be transferred to the CollectionViewController, and based on the solution the CollectionView will be built.

Below is the code I used for the start button. As you can see, the Algorithm is called, the results are stored in several variables and now I was trying to transfer the matrixArray to my CollectionViewController (its a first test). The CollectionViewController should use the data stored in this array to present some form of tableau

     @IBAction func startButton(_ sender: Any) {
    ...

    let solution = PrimalSimplex(problem: problem, currentSolution: currentSolution)

    matrixArray = solution.0
    basicArray = solution.1
    maxArray = solution.2
    currentSolutionArray = solution.3
    isOptimal = solution.4
    isCyceling = solution.5

    let CollectionVC = storyboard?.instantiateViewController(withIdentifier: "CollectionView") as! CollectionViewController
    CollectionVC.testMatrix = matrixArray
 }

So far so good, the data arrives is available in the CollectionViewController after the start button is pushed. But when I try to use the data to build the CollectionView, I get an error message.

This is the code that I used in the collectionViewController to build the CollectionView (It worked before, with static values... the problems occur when I try to use values that the algorithm returns):

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var myCollectionView: UICollectionView!

// Creates an empty array for the values  
var testMatrix = Array<Matrix>()

//Setup CollectionView: Table to display LPs
let reuseIdentifier = "cell"
var items = testMatrix[0]          <----ERROR

// MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = items[indexPath.item]
    cell.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.0) // make cell more visible in our example project

    // Change shape of cells
    cell.layer.cornerRadius = 8

    return cell
}
....

The error is displayed at var items = testMatrix[0]:

 Cannot use instance member 'testMatrix' within property initializer; property initializers run before 'self' is available

I can understand that Xcode has a problem here, because it can't be sure that the testMatrix has values stored.... I think that the problem. I tried to use if let/ guard statement, but that didnt solve the problem.

Any advice on how to fix it or whats actually wrong here? Maybe there is a better way to transfer the data from the first VC to the other one?

Bepa1012
  • 23
  • 6

1 Answers1

0

You can't initialize property from other dependent property at the class level.

You should try to initialized in viewDidLoad.

var items: Matrix?
override func viewDidLoad() {
    super.viewDidLoad()
    if testMatrix.count>0{
       items = testMatrix[0] 
    }

}
Salman Ghumsani
  • 3,491
  • 1
  • 17
  • 32
  • Thanks for the answer. It worked. But now I have a new Problem. My CollectionViewController didnt have the viewDidLoad Method before ( I followed this tutorial: https://stackoverflow.com/questions/31735228/how-to-make-a-simple-collection-view-with-swift ). I have added it, but when I click on the start button, the collectionView doesn't show up anymore. There is probably a very simple solution, but since I am new to programming, I have no clue what to do. Do you have an idea? – Bepa1012 Sep 25 '17 at 13:38
  • `let CollectionVC = storyboard?.instantiateViewController(withIdentifier: "CollectionView") as! CollectionViewController CollectionVC.testMatrix = matrixArray CollectionVC.items=matrixArray[0]` – Salman Ghumsani Sep 25 '17 at 13:39
  • Thanks again for the answer. This didn't work unfortunately. I was playing around a bit more with it... but so far no luck. It seems that when I click the start button, the data will be transferred to the CollectionVC (I used breaking points to see if data is correct), but after the segue the data will be reinitialized and there is no data to build the CollectionView from. Not sure how to solve this problem at the moment. Just to clarify, which VC contains the viewDidLoad Method? I tried it in both VCs but it seems to not solve the problem. – Bepa1012 Sep 26 '17 at 11:41
  • `CollectionViewController: UIViewController` contains `viewDidLoad ` method – Salman Ghumsani Sep 26 '17 at 11:43
  • Thanks again for the super fast answer. I tried it, but I get the error that testMatrix is an unresolved identifier. Im starting to think I programmed a huge mess here.... Learning to program is hard^^ – Bepa1012 Sep 26 '17 at 11:58
  • are u sure you define it `var testMatrix = Array()` in `CollectionViewController `? – Salman Ghumsani Sep 26 '17 at 12:07
  • Sorry I mixed up the variables... it says: matrixArray is an unresolved identifier. matrixArray is defined in the other VC and the variable contains the solution that is returned from the actual algorithm. I tried to move this discussion to chat but it seems to not work, because I have too little points... would you mind if i contact you in FB later (can't access it atm)? – Bepa1012 Sep 26 '17 at 12:17
  • Okay, no issue! – Salman Ghumsani Sep 26 '17 at 12:18