I have one struct, struct DropletsView: View {}
which has the following array: @State private var savedQuotes: [Quote] = []
(Quote
is defined elsewhere, as a custom type for my app).
I would like to pass in this savedQuotes
array to another struct (struct SingleQuoteView: View{}
), such that I can modify the array from within SingleQuoteView
.
I was trying to pass this in by reference, like you might do in C++, but ran into many issues along the way. Now, I would appreciate direction in trying to implement @Binding
for this array within SingleQuoteView
.
I’m declaring it as such: SingleQuoteView(quote: quote)
because of its constructor being this as of now:
init(quote: Quote) {
self.quote = quote
self._isBookmarked = State(initialValue: isQuoteBookmarked(quote))
self._isLiked = State(initialValue: isQuoteLiked(quote))
}
I’m assuming I have to create a variable with the same type in SingleQuoteView
, as such:
@Binding private var savedQuotes: [Quote]
init(quote: Quote, savedQuotesArg: Binding<[Quote]>) {
self.quote = quote
self._isBookmarked = State(initialValue: isQuoteBookmarked(quote))
self._isLiked = State(initialValue: isQuoteLiked(quote))
self._savedQuotes = savedQuotesArg
}
And declare that struct as such: SingleQuoteView(quote: quote, savedQuotesArg: $savedQuotes)
.
However, this gives the following error within the constructor (init()
):
Variable 'self.savedQuotes' used before being initialized