ios - touchesBegan 语法错误 - 没有成员
问题描述
基本上我要做的是测试用户是点击屏幕的左侧还是右侧。到目前为止,我过去对这段代码没有任何问题。然而,随着 Swift 4 的更新,它给我的语法带来了一些问题。不过,我已经解决了大部分问题。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.anyObject() as UITouch
let touchLocation = touch.location(in: self)
if touchLocation.x < self.frame.size.width / 2 {
// Left side of the screen
print("Left")
} else {
// Right side of the screen
print("Right")
}
}
这一条线我还不能修复。
let touch:UITouch = touches.anyObject() as UITouch
它给我的错误信息是:
'Set<UITouch>' 类型的值没有成员 'anyObject'
解决方案
你需要
guard let touch = touches.first else { return }
推荐阅读
- c++ - 在另一个类定义中为一个类创建对象
- asp.net - 在配置服务中获取服务实例(不是通过 services.BuildServiceProvider())
- r - 删除警告消息“在缓存中发现多个类“tbl_df”;使用第一个,来自命名空间 'tibble' 也由 'memisc' 定义
- node.js - 将 ghost CMS 安装到 AWS Elastic Beanstalk Node js 应用程序中
- ios - 如何判断objective-c中我的文件系统上有一个有效的pdf文件?
- arrays - 代码在使用指针时产生额外的一行并且不读取第一个字符数组
- java - nextInt 对这段代码中的 Scanner 类做了什么?
- logback - Logback:谨慎模式下基于大小的文件轮换
- ios - 无法在 Expo CLI 模拟器上运行 React Native App
- mysql - MySQL-在 SELECT 语句中创建重复的结果