所以我目前有一个将枚举作为类型的结构。下面是具有属性itemType的结构。
struct FormPickerItem: FormItem {
let id: String
var title: String
let itemType: Any.Type
var margin: UIEdgeInsets
var size: CGSize?
}
我的枚举的结构如下。
enum UserGoal {
case all
case buildMuscle
case strength
case none
}
extension UserGoal: EnumItem {
typealias RawValue = String
init?(rawValue: RawValue) {
switch rawValue {
case "All": self = .all
case "Build Muscle": self = .buildMuscle
case "Strength": self = .strength
case "None": self = .none
default: return nil
}
}
var rawValue: RawValue {
switch self {
case .all: return "All"
case .buildMuscle: return "Build Muscle"
case .strength: return "Strength"
case .none: return "None"
}
}
}
此结构是使用以下代码创建的,您可以看到itemType是我正在传递的枚举类型。
FormPickerItem(id: "userGoals", title: "Goals", itemType: UserGoal.self, margin: UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30), size: nil)
EnumItem
是一个协议,所有枚举都在子类化中,因此我可以轻松地将任何枚举作为通用类型传递给我拥有的自定义类。因此,我目前遇到的问题是我无法将结构中的属性作为通用类型传递。
func configure(with formPickerItem: FormPickerItem, atIndex index: Int = 0, toolBarType: ToolBarType? = .standard, delegate: FormTextFieldDelegate?) {
self.formPickerItem = formPickerItem
self.index = index
self.toolBarType = toolBarType
self.delegate = delegate
if loftyPicker == nil {
loftyPicker = LoftyPicker<formPickerItem.itemType>(index: index, title: formPickerItem.title, applyBottomBorder: true, parentView: self.contentView, loftyDelegate: self)
contentView.addSubview(loftyPicker!)
applyConstraints()
}
}
并且这样声明LoftyPicker类。
final class LoftyPicker<Item: EnumItem>: LoftyBaseTextField, UIPickerViewDelegate, UIPickerViewDataSource, LoftyToolBarDelegate {
....
}
我收到以下错误Use of undeclared type 'formPickerItem'
。这可能很快吗?如果没有,如何将枚举类型传递给类?