1

我正在开发一个包含许多具有默认值的自定义参数的包。鉴于有这么多潜在的初始化程序,当您更改许多参数时,正确排序所有内容很快就会变得混乱。

我有一个两部分的问题:

  1. 有没有办法从初始化中删除订购要求?
  2. 如果没有 - 有没有更好的方法来构造这个包的自定义参数?其他开发人员有什么建议?

我考虑过使用视图修饰符,但不确定在给定这个包结构的情况下它的工作效率如何。

结构初始化和使用如下。

public struct LineChart: View {
    public var data: [Double]
    public var title: String?
    public var subtitle: String?
    public var floatingPointNumberFormat: String
    public var cursorColor: Color
    public var curvedLines: Bool
    public var displayChartStats: Bool
    public var minWidth: CGFloat
    public var minHeight: CGFloat
    public var maxWidth: CGFloat
    public var maxHeight: CGFloat
    
    public var titleFont: Font
    public var subtitleFont: Font
    public var priceFont: Font
    public var fullScreen: Bool
    
    private var chartStyle: ChartStyle = Styles.lineChartStyleOne
    
    public init (data: [Double],
                 title: String? = nil,
                 subtitle: String? = nil,
                 style: LineChartStyle? = .primary,
                 curvedLines: Bool = true,
                 cursorColor: Color = Colors.IndicatorKnob,
                 displayChartStats: Bool = false,
                 minWidth: CGFloat = 0,
                 minHeight: CGFloat = 0,
                 maxWidth: CGFloat = .infinity,
                 maxHeight: CGFloat = .infinity,
                 titleFont: Font = .system(size: 30, weight: .regular, design: .rounded),
                 subtitleFont: Font = .system(size: 14, weight: .light, design: .rounded),
                 dataFont: Font = .system(size: 16, weight: .bold, design: .monospaced),
                 floatingPointNumberFormat: String = "%.1f",
                 fullScreen: Bool = false) {
        
        // Assign data
        self.data = data
        self.title = title
        self.subtitle = subtitle
        self.floatingPointNumberFormat = floatingPointNumberFormat
        self.cursorColor = cursorColor
        self.curvedLines = curvedLines
        self.displayChartStats = displayChartStats
        self.minHeight = minHeight
        self.minWidth = minWidth
        self.maxHeight = maxHeight
        self.maxWidth = maxWidth
        self.subtitleFont = subtitleFont
        self.titleFont = titleFont
        self.priceFont = dataFont
        self.fullScreen = fullScreen
        
        switch style {
        case .custom(let customStyle): self.chartStyle = customStyle
        case .primary: self.chartStyle = Styles.lineChartStyleTwo
        case .secondary: self.chartStyle = Styles.lineChartStyleThree
        case .tertiary: self.chartStyle = Styles.lineChartStyleFour
        default: self.chartStyle = Styles.lineChartStyleOne
        }
    }
    
    
    public var body: some View {
        LineChartView(data: self.data, title: self.title, legend: self.subtitle, style: self.chartStyle,  valueSpecifier: self.floatingPointNumberFormat, cursorColor: self.cursorColor, curvedLines: self.curvedLines, displayChartStats: self.displayChartStats, minWidth: self.minWidth, minHeight: self.minHeight, maxWidth: self.maxWidth, maxHeight: maxHeight, titleFont: self.titleFont, subtitleFont: self.subtitleFont, priceFont: self.priceFont)
    }
}
4

0 回答 0