快速解决这个问题。我在“Products.swift”类中有一个“products”数组。当我尝试将“products”数组访问到“SortLauncher.swift”类时 它的回报“为零” 这是我的代码
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)
}}
回复:无法排序。
答案 0 :(得分:1)
您必须传递parent(Products)类的对象,或者您必须在SortLauncher类中声明一个对象以保存Products类对象的引用以访问数据。我只是创建了demo来访问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)