Struct是fileprivate的,不能从swift 4.1中的默认参数值引用

Siv*_*rla 1 default-value swift swift4.1

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

 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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一种方法,其中将这些结构值作为默认参数传递。但是在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) {

}
Run Code Online (Sandbox Code Playgroud)

请在此处检查总代码。

解决此问题的方法是什么?

谢谢...

Kam*_*ran 5

如下所述有两个修复程序,

1)Defaults struct出来DropdownAlert,并使其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) {
    }
Run Code Online (Sandbox Code Playgroud)

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) {
}
Run Code Online (Sandbox Code Playgroud)

  • 我的意思是,在公开发布后,其工作迅速地进行了。更改之前它正在swift3中工作 (2认同)