我正在重构我的应用。它由始终相同的模型数据组成,所以我在函数parameterData()中定义了数据,但出现错误“属性初始化程序在self可用之前运行”
这是我重构应用程序时发生的。以前,productName是一个String数组,但是我将其更改为[ProductName]。 我完全迷路了。
我的模特:
import Foundation
struct Parameter {
var parameterName: String
var parameterSymbol: String
var imgName: String
var productName: [ProductName]
}
struct ProductName {
var name: String
}
视图控制器
class ParameterSelectionViewController: UICollectionViewController {
let productSegueIdentifier = "goToProductViewController"
private var flowLayout = ColumnFlowLayout()
private var parameters = parameterData()
override func viewDidLoad() {
super.viewDidLoad()
prepareCollectionView()
}
// MARK: - UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return parameters.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ParameterCell.identifier, for: indexPath) as! ParameterCell
// cell.parameter = parameters[indexPath.item]
return cell
}
let product1 = ProductName(name: "Item One")
let product2 = ProductName(name: "Item Two")
func parameterData() -> [Parameter] {
return[
Parameter(parameterName: "Alkalinity", parameterSymbol: "Kh", imgName: "alkalinity", productName: [product1, product2]),
Parameter(parameterName: "Calcium", parameterSymbol: "Ca", imgName: "calcium", productName: [product1, product2]),
Parameter(parameterName: "Magnesium", parameterSymbol: "Mg", imgName: "magnesium", productName: [product1, product2]),
Parameter(parameterName: "Phosphate", parameterSymbol: "PO3", imgName: "phosphate", productName: [product1, product2]),
Parameter(parameterName: "Nitrate", parameterSymbol: "NO3", imgName: "nitrate", productName: [product1, product2]),
]
}
我希望该应用程序能够编译,但出现标题中的错误。