-1

I am new to Swift and I am trying to understand how could a class get a value inside an extension.

I have two problems but they are similar to each other, but first I will explain my project. I am using 2 controllers (MainViewController and FactsViewController) that we could say first controller and second controller. The third file is a CollectionViewCell because I am using a collection view on my project.

So, for the first problem, I am trying to call prepareForSegue method to send a value to FactsViewController. I kinda have all the method done, but I don't know how could a I collect the indexPath from the extension to send the right information to my second screen. This is the method on my MainViewController:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
  let destinationVC = segue.destination as! FactViewController
  // getting the value associated with a variable here on this line
  destinationVC.receivedValue = // a possible variable with the right result
}

This is how my CollectionViewCell file looks like:

class FactsCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var lbFactsText: UILabel!
    @IBOutlet weak var lbCategories: UILabel!
    
    func setup(with facts: FactsData?, index: Int) {
        
        let result = facts!.result[index]
        
        lbFactsText.text = result.value
        print(result.value)
        if let category = result.categories.first {
            
            lbCategories.text = category!.uppercased()
            lbCategories.sizeToFit()
        } else {
            
            lbCategories.text = "UNCATEGORIZED"
            lbCategories.sizeToFit()
        }
    }
}

I don't if StackOverFlow will let I list all the code to be precise but here is the list of the extensions I am using:

extension MainViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    }
}

extension MainViewController: UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    }
}

extension MainViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    }
}

extension MainViewController: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    }
}

(Note that there are code inside those extension methods, it just listed empty just to show methods I am using).

How could I for example get a fact[result].value to transfer there on my prepareForSegue method? Also, as I mentioned two problems, how could I sum all heights of my lbFactsText and lbCategories to return on UICollectionViewDelegateFlowLayout. I am really confused, I heard that objc_setAssociatedObject or objc_getAssociatedObject could do this but I don't understand how to use it, it is really the correct way use this two methods? I got from this site: https://marcosantadev.com/stored-properties-swift-extensions/

Please let me know if you need me to list full details of the extensions.

virtu
  • 39
  • 5
  • 1
    Add a property to your MainViewController that you read in prepareForSegue and write to in your extension method. – Joakim Danielson May 02 '21 at 18:13
  • Now I see that also my `prepareForSegue` is not being called at all. My method `didSelectItemAt` is performing a `performSegue`, don't now if it is the problem. – virtu May 02 '21 at 18:49
  • Some how it works, but I identified that some crucial extensions protocol are being called before the ones that I need the values. I can not sum `lbFactsText` and `lbCategories` to use on `sizeForItemAt` because `sizeForItemAt` is called before `cellForItemAt`, then the app crash. – virtu May 02 '21 at 19:55

1 Answers1

0

Based on @Joakim Danielson, it worked. I had to write a property on my controller class to store the value inside the extension.

virtu
  • 39
  • 5