Expected Pattern error in Swift?

13k views Asked by At

I have been following Swift App Tutorial created by Apple here. Specifically, I am trying to set up a few custom buttons to create a rating control. The buttons have three images associated with it: emptyStar, filledStar, and highlightedStar, to be used at different states of the button.

I use the code exactly as provided by Apple, but I don't know why I have been getting this compile-time cryptic error Expected pattern as shown in the screenshot: Expected pattern errors Image Assets

What am I doing wrong here?

1

There are 1 answers

0
HuaTham On BEST ANSWER

Silly me, and Xcode, it's the Code Auto-Completion feature that confuses me.

The little icons in code shown in the first screenshot aren't nice visual decorations that match with my variable names (although I'd love these smart "emojis" decorating my code). They indicate that what I declare after let is not a variable, but the very image itself.

You have to be careful by not accepting the auto-completion when typing and stick with normal text when declaring the constant. After such declaration, you will see that there are two different, for example, emptyStar's of the same name -- one an image and another a constant.

Two empty stars

Here's the final code, barely noticeable visual change!

Final working code