Anytime I have 1 item, then add another, the app crashes with the following error:
Swift/IntegerTypes.swift:8835: Fatal error: Double value cannot be converted to Int because it is either infinite or NaN
I'm not working with Int values though, so I'm not sure why this is happening. It's also not point me towards any specific line. I've printed out all of the values being used for the chart and none of them are showing as infinite or NaN.
The data being used is created in a View.
@State private var pieChartData: [PieChartDataPoint] = []
Then in the onAppear and onChange its gets loaded using the following function:
func getPieChartDataPoints() -> [PieChartDataPoint] {
self.map({ PieChartDataPoint(label: $0.name, value: $0.monthlyAmount()) })
}
That's an extension on a SwiftData model type I have called Recurring.
@Model
final class Recurring {
var id = UUID()
enum Kind: String, CaseIterable, Codable {
case income = "Income"
case bill = "Bill"
func plural() -> String {
switch self {
case .income:
"Income"
case .bill:
"Bills"
}
}
}
enum Frequency: String, CaseIterable, Codable, Identifiable {
var id: Frequency { self }
case weekly = "Weekly"
case biweekly = "Biweekly"
case monthly = "Monthly"
case quarterly = "Quarterly"
case annually = "Annually"
}
var name: String = ""
var kind: Kind = Kind.income
var frequency: Frequency = Frequency.biweekly
var amount: Double = 0
init(name: String, kind: Kind, frequency: Frequency, amount: Double) {
self.name = name
self.kind = kind
self.frequency = frequency
self.amount = amount
}
func amount(from cycle: Cycle) -> Double {
switch cycle {
case .weekly:
monthlyAmount() * 12 / 52
case .biweekly:
monthlyAmount() * 12 / 26
case .monthly:
monthlyAmount()
}
}
func monthlyAmount() -> Double {
switch frequency {
case .weekly:
amount * 52 / 12
case .biweekly:
amount * 26 / 12
case .monthly:
amount
case .quarterly:
amount * 4 / 12
case .annually:
amount / 12
}
}
}
Here is the DataPoint structure.
struct PieChartDataPoint: Identifiable, Equatable {
var id = UUID()
var label: String
var value: Double
}
Finally here is the chart.
Chart(sorted, content: { dataPoint in
SectorMark(
angle: .value(dataPoint.label, getValue(from: dataPoint)),
innerRadius: 50,
angularInset: 5
)
.foregroundStyle(by: .value("Label", dataPoint.label))
.cornerRadius(10)
.opacity(getSectorMarkOpacity(for: dataPoint))
})
.scaledToFill()
.chartLegend(.hidden)
.chartAngleSelection(value: $chartSelection)
.onChange(of: chartSelection, checkSelection)
For testing purposes, I have removed all of the modifiers and only had a simple SectorMark in the Chart, however, that was still crashing.
Does anyone know why this is happening?