I have two quote dictionaries I’m trying to append after an in-app purchase. I have tried several different methods to append the dictionaries together but I’m still getting an error "No exact matches in call to instance method ‘append’"
I have established variables for each array to then append the array within the struct.
Any thoughts? Is there a better method I should use to add the quotes from the array called QuoteDetails2 to the initial array QuoteDetails?
var topQuotes = [QuoteDetails]()
var additionalQuotes = [QuoteDetails2]()
public struct QuoteProvider {
static func all() -> [QuoteDetails] {
[
QuoteDetails(
id: "1",
texts: "High school is fun",
authors: "SM"
),
QuoteDetails(
id: "2",
texts: "Middle School is fun",
authors: "A. Philip Randolph"
),
QuoteDetails(
id: "3",
texts: "The playground is fun",
authors: "Booker T. Washington"
),
QuoteDetails(
id: "4",
texts: "Hold on to your dreams of a better life and stay committed to striving to realize it.",
authors: "KJ"
)
]
}
static func all2() -> [QuoteDetails2] {
[
QuoteDetails2(
id: "1",
texts: "The cat ran fast",
authors: " ME"
),
QuoteDetails2(
id: "2",
texts: "The dog ran fast.",
authors: " ME"
),
QuoteDetails2(
id: "3",
texts: "End life problems",
authors: "ME"
)
]
}
func showPremiumQuotes() {
if UserDefaults.standard.bool(forKey: "premiumQuotes") == true {
topQuotes.append(contentsOf: additionalQuotes)
}
}
/// - Returns: A random item.
static func random() -> QuoteDetails {
let allQuotes = QuoteProvider.all()
let randomIndex = Int.random(in: 0..<allQuotes.count)
return allQuotes[randomIndex]
}
}
3
Answers
This should be a comment but it would be a bit tricky to explain what Koropok & Leo Dabus were saying without the formatting.
You have two arrays:
When you try to do this line
topQuotes.append(contentsOf: additionalQuotes)
, the compiler gives you the error becausetopQuotes
is expecting a typeQuoteDetails
to be stored in it where asadditionalQuotes
says it is storingQuoteDetails2
so it is unable to append a different type totopQuotes
.Just because they have the exact same properties, the compiler cannot tell they are the same because they are named differently. It is kind of doing something like this:
A quick fix to get this to work is to say
topQuotes
can storeAny
type, like thisBut I recommend against this, just showing you your options and if you are new, I would stay away from this for now.
Based off of this and your
QuoteProvider
struct, it seems you have 2 structs which are identical:What is the difference between
QuoteDetails
andQuoteDetails2
? If nothing, then you can get rid ofQuoteDetails2
and changeto
Once you define a type, you can reuse that type multiple times.
After you make the above change, I believe the errors should go away.
you can have both
QuoteDetails
andQuoteDetails2
in same array if it’sAny
To different QuoteDetails from QuoteDetails2 in Array of Any
you doesn’t append a dictionary array to another dictionary array, but you are appending to array with different type
If you want to have two separate arrays with the possibility to merge them you can add an init to one of the structures that takes the other struct as an argument.
and then add another static function that returns both arrays as a single one
Another option is to create a protocol that both structures conform to so that you can use a common array of that protocol type
and then a function that creates the array