I have this model:
struct MemeModel: Codable {
var id: Int
var title: String
var nickname: String
}
And try to pass an initialized custom Array from preview to view:
struct Comments_Previews: PreviewProvider {
static var previews: some View {
Comments(memes: arrayOf(MemeModel(123, "blaaa", "joooo"),MemeModel(456, "hhhhh", "dddd")),memeid: 777)
}
}
I get: Cannot find 'arrayOf' in scope
This is in the Comments
view:
@Binding var memes: [MemeModel]
@Binding var memeid: Int
3
Answers
As Joakim pointed out in his comment, there is no
arrayOf
in swift. Probably you remember this from Kotlin.You should construct your MemeModel array as follows:
Init it as @State property
In Swift you can just use
[element, element, element]
to create a literal array. Your view needs a@Binding
, so you can’t just pass the array. You need to convert it to a bound value. You can useBinding.constant
, abbreviated to.constant
to create a constant binding.