Reputation: 45
This is my first time building an app using SwiftUI and Xcode is giving me strange errors as I attempt to create the interface.
What I ultimately want will be a list that iterates over an array of dice (Die being a struct I created to define a particular die, like a d6 or d20).
For now, I'm just testing the interface. For the purposes of testing/previewing in Xcode, I'd like to be able to use some test data, but I can't quite figure out how to pass that variable into the main view. I'm getting a warning that "the result of the initializer is unused" and an error that "Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type".
I've probably done something stupid. Can anyone help illuminate what?
struct DiceListView: View {
var diceToList: [Die]
var body: some View {
Text("testing")
}
init(dice: [Die]) {
diceToList = dice
}
}
struct DiceListView_Previews: PreviewProvider {
static var previews: some View {
// setup test dice data (d4, d5, d6)
let testDice: [Die] = [Die(sides: 4), Die(sides: 5), Die()]
DiceListView(dice: testDice)
}
}
Upvotes: 1
Views: 1625
Reputation: 257693
It cannot detect return type automatically in this case, so here are possible fixes
static var previews: some View {
// setup test dice data (d4, d5, d6)
let testDice: [Die] = [Die(sides: 4), Die(sides: 5), Die()]
return DiceListView(dice: testDice)
}
or
static var previews: some View {
// setup test dice data (d4, d5, d6)
DiceListView(dice: [Die(sides: 4), Die(sides: 5), Die()])
}
Upvotes: 2