swift2 “运算式清单中必须有运算式”

dy1byipe  于 2022-11-06  发布在  Swift
关注(0)|答案(6)|浏览(154)

我正在尝试使用一些代码来制作一个使用Xcode 7.3和Swift 2.2的初学者应用程序,但我一直遇到同样的问题。我以前也用过类似的代码,但这就是不起作用。出现的错误信息是“预期”,“separator”,当我修复它时,同样的消息一次又一次出现。我还得到“表达式列表中需要表达式”和“调用””中缺少参数“action”的参数。它们都是由同一行引起的

button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)

下面是代码

import UIKit

class RatingControl: UIView {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        button.backgroundColor = UIColor.redColor()
        button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
        addSubview(button)
    }

    override func intrinsicContentSize() -> CGSize {
        return CGSize(width: 240, height: 44)
    }

    func ratingButtonTapped(button: UIButton) {
        print("Button pressed")
    }
}
gv8xihay

gv8xihay1#

您需要 * 清理 * 项目。
按Shift ++ K组合键

9lowa7mx

9lowa7mx2#

#selector是Xcode 7.3附带的Swift 2.2,所以你必须将Xcode更新到最新版本。
完成后,如果您的Xcode在升级后感到困惑,请帮助它:进入菜单“产品〉清理”,如果需要的话,也清理“衍生数据”文件夹。

  • 注意:这是我对OP的评论中的回答,解决了他们的问题。*
gtlvzcf8

gtlvzcf83#

您可以尝试以下代码:

button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
gxwragnw

gxwragnw4#

我在创建UIButton时遇到了类似的问题,如下所示:

let button = UIButton()
button.frame = CGRect(x: 0.0, y: 0.0, width: size, height: size)

对我来说,当我改变按钮的创建方式时,错误就消失了:

let button = UIButton(type: .System)
button.frame = CGRect(x: 0.0, y: 0.0, width: size, height: size)

无论是清理项目还是删除派生数据都没有为我解决这个问题。我使用的是Xcode 7.3.1。

o0lyfsai

o0lyfsai5#

您应该清理项目:
产品-〉清洁

vwkv1x7d

vwkv1x7d6#

my code是true的一个代码:

button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)

但是,什么区别betwen行动:字符串和操作:#选择器

相关问题