I'm really new to Swift, and am making a simple app to track pellets eaten by my pet axolotl Miyamoto. I am trying to represent the pellet count visually using the Swift Charts Framework. However, I can't quite figure out how to solve this error that I am getting.
Property definition has inferred type 'State<Chart>', involving the 'some' return type of another declaration`
I get it whenever the ForEach loop is present, even though my code is quite simillar to this example. Removing the ForEach loop lets the code build and the build it works fine. I have done some reading on this and some is used when declaring a function, but I have no references to some in my ForEach loop. I have read this post, but no answers there seem to be relevant to my problem. I have done a lot of googling about this but there is still no solution that works. Here are the relevant parts of my code:
...
struct PelletArr: Identifiable {
var pelletdata: Array<String>
let id: Int = 987654321 // just put some number here to stop errors
}
......
struct ContentView: View {
......
@State var charliechart: Chart = Chart {
var pelletarrarr: Array<PelletArr> = []
var _ = pelletstor.stor.forEach { item in
pelletarrarr.append(PelletArr(pelletdata: item))
}
ForEach(pelletarrarr, id: \.pelletdata[0]) { pelletd in
LineMark(
x: .value("Date", DateString().dateNumber(dateStr: pelletd.pelletdata[0])),
y: .value("Pellet Count", pelletd.pelletdata.count),
series: .value("Axolotl", "A")
)
.foregroundStyle(.green)
}
RuleMark(
y: .value("Fixed", 10)
)
}
......
}
NOTES:
PelletArris a wrapper of the actual array, which is stored in the propertypelletdata......indicates code I deemed irrelevent- The formatting of the data storage is
Array<Array<String>>, here is a small example:
[
["1/1/2003", "", "", "", ""]
^ ^ ^ ^ ^
the date these are all
(Australia blank strings,
format) 1 equals 1
pellet
]
- The data is parsed from strings, which is stored in the Swift 'defaults'.
Since you are very new to SwiftUI, try this example code using a simple
Chart{...}call inside the view body. The data is represented as an array ofstruct Pellet, that you load when the View appears.Read more about how to make charts here: Charts
Also read about using @State in SwiftUI.
Re your last comment,
The data is parsed from strings, which is stored in the Swift 'defaults'.Don't store this kind of data in theUserDefaults, it is only meant to be used for small bits of setting data.EDIT-1
If you really want to have a separate variable for your
charliechart, then use this example code: