SwiftUI ERR: "Argument 'title' must precede argument 'style'

154 views Asked by At

I'm working on a package that has many custom parameters with default values. Given that there are so many potential initializers it can quickly become confusing to properly order everything when you change many parameters.

I have a two part question:

  1. Is there a way to remove the ordering requirement from initialization?
  2. If not – is there a better way to structure custom parameters for this package? What do other developers recommend?

I considered using view modifiers but unsure how effectively that'd work given this package structure.

Struct initialization & utilization are below.

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)
    }
}
0

There are 0 answers