I came to this problem when I tried to append coffeeShop to allShops[coffeeShop]. How to solve this?
Here is my coffeeShop class
It is generally preferred to capitalize the names of types. It doesn’t make any semantic difference, but in a case like this it would make it much more obvious what the problem is. (Though the errors and warnings still do a good job of explaining it.)
So first, I would recommend renaming struct coffeeShop
to struct CoffeeShop
. Then the problem code would look like this:
CoffeeShop( … ) // warning: result unused
allShops.append(CoffeeShop) // error: cannot convert to expected type
Do you see the problem now?
Also, this is more suited to be on Using Swift category. Evolution is about pitching new ideas and changes to the language (e.g. new syntax).
Putting it in the “right” category will help attract the right group of people that can help :)
I also recommend you check out the design guideline.
It outlines a few conventions, including CamelCasing of names amongst other things.
I have capitalized them, but the problem remains the same. How can I fix it?
Noticed. Thanks for your advice.
The problem is that you’re not appending a value of CoffeeShop
, you’re appending the CoffeeShop
class.
Try this
let coffeeShop = CoffeeShop(id: id, name: name, ....)
allShops.append(coffeeShop)
This will create a value of type CoffeeShop
named coffeeShop
(this is also why casing is important). And append coffeeShop
instead.
This is because CoffeeShop
is just a name of the class, it doesn’t hold any data like id
, name
, latitude
. And you’re trying to append a dataless CoffeeShop
which is not what the array wants.
Instead the you append a value of CoffeeShop
that is filled with data (aka. an instance of CoffeeShop
), that’s why I saved the one you created into coffeeShop
, and append that one instead.
Perfectly handled. Thanks a lot
Also this is not a class. ;)