Передать свойство struct как универсальный тип - PullRequest
0 голосов
/ 15 октября 2018

Итак, в настоящее время у меня есть структура, которая принимает перечисление как тип.Ниже приведена структура со свойством 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 - это протокол, в котором все перечисления являются подклассами, так что я могу легко передать любой Enum как универсальный тип в свой собственный класс.Итак, проблема, с которой я сейчас сталкиваюсь, заключается в том, что я не могу передать свойство в своей структуре как универсальный тип.

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'.Возможно ли это в кратчайшие сроки?Если нет, то как я могу передать тип enum в класс?

...