Cannot convert value of type '(ViewController.coffeeShop).Type' to expected argument type 'ViewController.coffeeShop'

I came to this problem when I tried to append coffeeShop to allShops[coffeeShop]. How to solve this?

Here is my coffeeShop class
28

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?

1 Like

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 :)

1 Like

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.

1 Like

Perfectly handled. Thanks a lot :grinning:

Also this is not a class. ;)