4

很快就遇到了麻烦。我在“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)
}}

响应:无法排序。

4

1 回答 1

1

您必须传递父(产品)类的对象,或者您必须在 SortLauncher 类中声明一个对象来保存 Products 类对象的引用以访问数据。我只是创建了演示来访问 SortLauncher 类中产品类的所有变量。导入基础导入 UIKit

class Products: NSObject
{
    var products: [String]?
    {
        didSet
        {
            filterPro = products
        }
    }
    var filterPro : [String]?
    {
        didSet
        {
        }
    }}

class SortLauncher: NSObject
{

    func sort(parent:Products)
    {

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



    }
    override init()
    {
            super.init()
    }
}
let productObject:Products = Products.init()
productObject.products = ["a","b","c","d"];

let sObject:SortLauncher = SortLauncher.init()
sObject.sort(parent: productObject)
于 2017-07-20T03:23:39.547 回答