3

我使用这样的默认值结构。

 fileprivate struct Defaults {

    static var BackgroundColor = UIColor.white
    static var TextColor = UIColor.black
    static var Title = "Default Title"
    static var Message = "Default message!"
    static var AnimationDuration: Double = 0.25
    static var Duration: Double = 2
    static var Height: CGFloat = 90
    static var TitleFont: UIFont = UIFont(name: "SourceSansPro-Semibold", size: Defaults.FontSize)!
    static var MessageFont: UIFont = UIFont(name: "SourceSansPro-Regular", size: Defaults.FontSize)!
    static var FontSize: CGFloat = 14 {
        didSet {
            TitleFont = TitleFont.withSize(FontSize)
            MessageFont = MessageFont.withSize(FontSize)
        }
    }
}

我有一个方法可以将这些结构值作为默认参数传递。但在 swift4 中它不起作用。

 class func showWithAnimation(_ animationType: AnimationType = .basic(timingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)),
                             title: String = Defaults.Title,
                             message: String = Defaults.Message,
                             backgroundColor: UIColor = Defaults.BackgroundColor,
                             textColor: UIColor = Defaults.TextColor,
                             duration: Double = Defaults.Duration) {

}

请在此处查看总代码。

有什么办法解决这个问题?

谢谢...

4

1 回答 1

10

有两个修复,如下所述,

1)取出Defaults structDropdownAlert使其public均匀properties,因为您想在方法签名中传递它们,如下所示,

public struct Defaults {
    public static var BackgroundColor = UIColor.white
    public static var TextColor = UIColor.black
    public static var Title = "Default Title"
}

class func showWithAnimation(_ animationType: AnimationType = .basic(timingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)),
                                     title: String = Defaults.Title,
                                     message: String = Defaults.Message) {
    }

2)保持Defaults在里面DropdownAlert,但也public包括在内properties。并访问如下,

class func showWithAnimation(_ animationType: AnimationType = .basic(timingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)),
                                 title: String = DropdownAlert.Defaults.Title,
                                 message: String = DropdownAlert.Defaults.Message) {
}
于 2018-05-15T07:52:38.137 回答