多个默认参数调用的语法

Syntax for multiple default parameter call

我正在尝试制作一个 "Label Factory" 来帮助我为项目制作标签。

我有一个功能

getLabel(of type: LabelTypeEnum,
                         _ labelTitle: String? = nil,
                         _ textColor: UIColor? = nil,
                         _ textAlignment: NSTextAlignment? = nil) -> Label 

然后我想用任意数量的参数调用它,例如:

getLabel(of: .myLabel, .center)

导致错误:“类型 'String?' 没有成员 'center' “

如何告诉编译器 .center 不是 labelTitle 属性?

我试图通过添加 "alignment: .center" 来指定内容,但它迫使我删除它。

如果有任何帮助,我会在下面添加代码的上下文。

static func getLabel(of type: LabelType,
                          _ labelTitle: String? = nil,
                          _ textColor: UIColor? = nil,
                          _ textAlignment: NSTextAlignment? = nil) -> Label {
        switch type {

        case .regular:
            return RegularLabel(labelTitle, textColor, textAlignment)

        case .title:
            return TitleLabel(labelTitle, textColor, textAlignment)
        case .description:
            return DescriptionLabel(labelTitle, textColor, textAlignment)
        case .header:
            return HeaderLabel(labelTitle, textColor, textAlignment)

        case .subHeader:
            return SubHeaderLabel(labelTitle, textColor, textAlignment)

        case .body:
            return BodyLabel(labelTitle, textColor, textAlignment)

        case .fatBody:
            return FatBodyLabel(labelTitle, textColor, textAlignment)

        case .smallGray:
            return SmallGrayLabel(labelTitle, textColor, textAlignment)
        }
    }

标签初始化

convenience init(_ title: String? = nil, _ color: UIColor? = nil, _ alignment: NSTextAlignment? = nil) {
        self.init(frame: .zero)
        self.text = title ?? self.text
        self.textColor = color ?? self.textColor
        self.textAlignment = alignment ?? self.textAlignment
    }

许多电话中的一个

private lazy var titleLabel: UILabel = {
        let label = LabelFactory.getLabel(of: .header, .center)
        self.addSubview(label)
        return label
    }()

这是为了方便省略参数标签的诅咒,这应该有效:

getLabel(of type: LabelTypeEnum,
      labelTitle: String? = nil,
       textColor: UIColor? = nil,
   textAlignment: NSTextAlignment? = nil) -> Label

getLabel(of: .myLabel, textAlignment: .center)