How to access an array from another class/file in Swift

Viewed 2106

Having trouble with this in swift. I have a “products" array in “Products.swift” class. When i try to access “products” array into “SortLauncher.swift” class its returns “nil” Here is my code

Products.swift

import Apollo
class Products: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchResultsUpdating, UISearchBarDelegate {
   var products: [ProductsQuery.Data.Node.AsCollection.Product.Edge]? {
    didSet {
        filterPro = products
        collectionView?.reloadData()
    }
}
 var filterPro : [ProductsQuery.Data.Node.AsCollection.Product.Edge]? {
    didSet {
    }
}}

SortLauncher.swift

class SortLauncher: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

  var sortProducts = Products()

    if let sorted = sortProducts.products {
        print("Sorted Products \(sorted.count).")
    } else {
        print("Unable to sort.")
    }

    handleGesture()

}
override init() {
    super.init()
    sortingList.dataSource = self
    sortingList.delegate = self

    sortingList.register(SortListCell.self, forCellWithReuseIdentifier: cellId)
}}

Response : Unable to sort.

1 Answers
Related