我想知道最简单的代码,当点击数字键盘外的任何地方时,它会关闭数字键盘键盘.在文本字段中输入数字是一个简单的应用程序,然后用户可以在用户完成在文本字段上键入数字后关闭键盘.谢谢!:)
XCode 6中的此代码没有错误,但在XCode 7(Swift 2)中发生了此错误:
方法不会覆盖其超类中的任何方法
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
}
Run Code Online (Sandbox Code Playgroud)
删除override单词时发生此错误:
方法'的touchesBegan(::) withEvent'与目标C选择'的touchesBegan:withEvent:方法'与方法冲突'的touchesBegan( :从超类的UIResponder'withEvent :)'具有相同的目标C选择
我正在检查是否已选择元素.
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
// First, see if the game is in a paused state
if !gamePaused
{
// Declare the touched symbol and its location on the screen
let touch = touches.anyObject! as? UITouch
let location = touch.locationInNode(symbolsLayer)
Run Code Online (Sandbox Code Playgroud)
这个以前在Xcode 6.2中编译得很好但是有了6.3更新,"let touch = touches.anyObject!as?UITouch"这一行引发了错误:
'Set'没有名为'anyObject'的成员
我已经阅读了许多类似的问题,但我似乎无法理解"要使用价值,你需要首先解开".特别是因为答案似乎集中在通知上.
非常感谢.w ^
今天我用Swift 1.2更新了xcode我的代码在Swift 1.1上工作得很好但是当我更新时我得到了这个错误:
'Set'没有名为'anyObject'的成员
这是我的代码:
override public func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let location:CGPoint? = touches.anyObject()?.locationInView(self)
if let loc = location {
if (!CGRectContainsPoint(ScaleRect(self.bounds, n: 2.0), loc)) {
self.highlighted = false
}
}
}
Run Code Online (Sandbox Code Playgroud)
你们有一个想法,我该如何解决这个问题?
我检查了我的旧游戏,我想在Swift 2.0中更新它.当我试图修复它时,Xcode发现了一个错误.错误是类型'Set'的值在此代码行中没有成员'anyObject':
var touch:UITouch = touches.anyObject() as! UITouch
Run Code Online (Sandbox Code Playgroud)
功能:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.runAction(SKAction.playSoundFileNamed("torpedo.mp3", waitForCompletion: false))
var touch:UITouch = touches.anyObject() as! UITouch //<-- Here is Error
var location:CGPoint = touch.locationInNode(self)
var torpedo:SKSpriteNode = SKSpriteNode(imageNamed: "torpedo")
torpedo.position = player.position
torpedo.physicsBody = SKPhysicsBody(circleOfRadius: torpedo.size.width/2)
torpedo.physicsBody!.dynamic = true
torpedo.physicsBody!.categoryBitMask = photonTorpedoCategory
torpedo.physicsBody!.contactTestBitMask = alienCategory
torpedo.physicsBody!.collisionBitMask = 0
torpedo.physicsBody!.usesPreciseCollisionDetection = true
var offset:CGPoint = vecSub(location, b: torpedo.position)
if (offset.y < 0){
return
self.addChild(torpedo)
var direction:CGPoint = …Run Code Online (Sandbox Code Playgroud) 当我收到此错误时,我正在尝试创建一个新的覆盖功能:方法不会覆盖它的超类中的任何方法
我已经尝试删除覆盖部分,但这只会给我一个错误.
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let touch : UITouch! = touches.anyObject() as! UITouch
location = touch.locationInView(self.view)
person.center = location
}
override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
let touch : UITouch! = touches.anyObject() as! UITouch
location = touch.locationInView(self.view)
person.center = location
}
Run Code Online (Sandbox Code Playgroud) 好的,Swift 1.2发生了什么?我最近更新并突然发现我的项目全是红色的.是否已删除与NSSets的兼容性或什么?我该如何解决?
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
Run Code Online (Sandbox Code Playgroud)
结果:
Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'
Run Code Online (Sandbox Code Playgroud) swift ×6
ios ×3
swift2 ×2
xcode ×2
iphone ×1
member ×1
named ×1
nsobject ×1
numbers ×1
object ×1
set ×1
skspritenode ×1
sprite-kit ×1
touchesbegan ×1
touchesmoved ×1
uitextfield ×1
uitouch ×1