Reputation: 89
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.
Upvotes: -1
Views: 163
Reputation: 89
Based on @Joakim Danielson, it worked. I had to write a property on my controller class to store the value inside the extension.
Upvotes: 0