结构是文件私有的,不能从 swift 4.1 中的默认参数值引用
Struct is fileprivate and cannot be referenced from a default argument value in swift 4.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)
}
}
}
我有一个方法可以将这些结构值作为默认参数传递。但在 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) {
}
请检查总码here。
解决这个问题的方法是什么?
谢谢...
有两个修复如下所述,
1) 从 DropdownAlert
中取出 Defaults
struct
并使其成为 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) {
}
我正在使用具有这样默认值的结构。
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) {
}
请检查总码here。
解决这个问题的方法是什么?
谢谢...
有两个修复如下所述,
1) 从 DropdownAlert
中取出 Defaults
struct
并使其成为 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) {
}