Xcode 6.3.在实现UITextFieldDelegate协议的类中,我想重写touch chesBegan()方法以可能隐藏键盘.如果我在函数规范中避免了编译器错误,那么try 从Set或NSSet读取"touch"时会出现编译器错误,否则super.touch chesBegan(touch,with Event:event)会抛出错误.这些组合之一是用Xcode6.2编译的!(那么,SWIFT的文档在哪里"设置",以及如何从其中获取元素?)

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    // Hiding the Keyboard when the User Taps the Background
        if let touch =  touches.anyObject() as? UITouch {
            if nameTF.isFirstResponder() && touch.view != nameTF {
                nameTF.resignFirstResponder();
            }
        }
        super.touchesBegan(touches , withEvent:event)
    }

try :

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) or
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) 

Compiler error: Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()' and

super.touchesBegan(touches , withEvent:event)

也在抱怨

"NSSet"不能隐式转换为"Set";是否要使用"As"显式转换?

try :

override func touchesBegan(touches: Set<AnyObject>, withEvent event: UIEvent) 

编译器错误: 类型"AnyObject"不符合协议"Hasable"

try :

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 

编译器错误在

if let touch = touches.anyObject() as? UITouch 

"set"没有名为"anyObject"的成员,但是函数规范和对Super()的调用都是正常的!

try :

override func touchesBegan(touches: NSSet<AnyObject>, withEvent event: UIEvent) -> () or
override func touchesBegan(touches: NSSet<NSObject>, withEvent event: UIEvent) 

编译器错误: 无法专门化非泛型类型""NSSet""

推荐答案

Swift 1.2 (Xcode 6.3)引入了本机Set类型,该类型可桥接 有NSSet个.这一点在Swift blogXcode 6.3 release notes,但显然还没有添加到官方文档中(更新:AS Ahmad Ghadiri noted,它现在记录了is).

UIResponder方法现在声明为

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)

您可以这样覆盖它:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches.first as? UITouch {
        // ...
    }
    super.touchesBegan(touches , withEvent:event)
}

Update for Swift 2 (Xcode 7):(比较Override func error in Swift 2)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, withEvent:event)
}

Update for Swift 3:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, with: event)
}

Ios相关问答推荐

如何在PINCH上添加UITextfield以zoom 图像

无法在所有窗口上方显示图像

在某些情况下,UIHostingController在自动调整时不能调整大小

如何在wiftUI中管理导航栈?

用于HDR显示的CI上下文选项

更改初始控制器无效

在 Swift 中 @MainActor 似乎被 withCheckedContinuation 而不是其他异步调用继承是巧合吗?

过渡动画在 iOS16 中不起作用,但在 iOS15 中起作用

Text() 正在添加额外的前导尾随填充 SwiftUI

集成 IDNow SDK 时未找到 Xcode FLAnimatedImage.h 文件问题

使用 Vim 代替(或与)Xcode 进行 iOS 开发

本月使用 iOS 的天数?

如何使用 swift 显示 .svg 图像

Xcode 缺少支持文件 iOS 12.2 (16E227)

没有 Mac 的 Xamarin Visual Studio IOS 开发?

如何在 OS X 或 iOS(不使用格式塔)中确定运行时的操作系统版本?

如何在 iOS 中判断暗模式?

Storyboard Segue 从视图控制器到自身

如何使用完成按钮制作 UIPickerView?

在 NSUserDefaults 中存储值是否有任何限制?