很快就遇到了麻烦。我在“Products.swift”类中有一个“products”数组。当我尝试将“products”数组访问到“SortLauncher.swift”类时,它返回“nil”这是我的代码
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 {
}
}}
排序启动器.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)
}}
响应:无法排序。