简体   繁体   English

Swift中的预期模式错误?

[英]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. 按钮有三个与之关联的图像:emptyStar,filledStar和highlightStar,用于按钮的不同状态。

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: 我使用完全由Apple提供的代码,但我不知道为什么我得到这个编译时隐藏错误Expected pattern如截图所示: 预期的模式错误 图像资产

What am I doing wrong here? 我在这做错了什么?

Silly me, and Xcode, it's the Code Auto-Completion feature that confuses me. 愚蠢的我和Xcode,这是代码自动完成功能让我感到困惑。

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. 它们表明我在let之后声明的不是变量,而是图像本身。

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. 在这样的声明之后,你会看到有两个不同的,例如, emptyStar - 一个是图像而另一个是常量。

两颗空星

Here's the final code, barely noticeable visual change! 这是最终的代码,几乎没有明显的视觉变化!

最终的工作代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM