Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected Pattern error in Swift?

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?

like image 420
HuaTham Avatar asked Dec 26 '16 11:12

HuaTham


1 Answers

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

like image 61
HuaTham Avatar answered Oct 11 '22 02:10

HuaTham