Swift 2: type 'Set'的值没有成员'anyObject'[复制]

时间:2023-01-23 08:36:05

This question already has an answer here:

这个问题已经有了答案:

I checked my old game and I want to update it in Swift 2.0. When I tried to fix it, Xcode found an error. Error is Value of type 'Set' has no member 'anyObject' on this line of code:

我检查了我的旧游戏,我想更新它在Swift 2.0。当我试图修复它时,Xcode发现了一个错误。错误是“Set”类型的值在此代码行上没有成员“anyObject”:

var touch:UITouch = touches.anyObject() as! UITouch

function:

功能:

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 = vecNormalize(offset)

    var shotLength:CGPoint = vecMult(direction, b: 1000)

    var finalDestination:CGPoint = vecAdd(shotLength, b: torpedo.position)

    let velocity = 568/1
    let moveDuration:Float = Float(self.size.width) / Float(velocity)

    var actionArray:NSMutableArray =  NSMutableArray()
    actionArray.addObject(SKAction.moveTo(finalDestination, duration: NSTimeInterval(moveDuration)))
    actionArray.addObject(SKAction.removeFromParent())

    torpedo.runAction(SKAction.sequence(actionArray))
}

So what to fix here ?

那么,这里要解决什么问题呢?

1 个解决方案

#1


8  

Given a Set of UITouch defined as follow

给定一组UITouch,定义如下

touches: Set<UITouch>

you can retrieve a touch with this code

您可以使用此代码获取一个触摸。

let touch = touches.first

Generics

There is no need to force cast the retrieved element to UITouch. Infact now the Apple APIs written in Objective-C have been updated with generics. This means that in this case you receive a Set of UITouch (not a Set of NSObject that could literally contains any object). So the Set already knows the contained elements are UITouch.

不需要强制将检索到的元素强制转换为UITouch。实际上,现在用Objective-C编写的苹果api已经用泛型进行了更新。这意味着在这种情况下,您将收到一组UITouch(不是一组NSObject,它可以包含任何对象)。所以集合已经知道包含的元素是UITouch。

Optionals

The first computed property does return an Optional. Infact if the Set is empty it does return nil.

第一个计算属性确实返回一个可选的。事实上,如果集合为空,它会返回nil。

A good and safe technique to unwrap the element is

一种很好的、安全的方法来展开元素

guard let touch = touches.first else {
    debugPrint("Ops, no touch found...")
    return
}

// here you can use touch

#1


8  

Given a Set of UITouch defined as follow

给定一组UITouch,定义如下

touches: Set<UITouch>

you can retrieve a touch with this code

您可以使用此代码获取一个触摸。

let touch = touches.first

Generics

There is no need to force cast the retrieved element to UITouch. Infact now the Apple APIs written in Objective-C have been updated with generics. This means that in this case you receive a Set of UITouch (not a Set of NSObject that could literally contains any object). So the Set already knows the contained elements are UITouch.

不需要强制将检索到的元素强制转换为UITouch。实际上,现在用Objective-C编写的苹果api已经用泛型进行了更新。这意味着在这种情况下,您将收到一组UITouch(不是一组NSObject,它可以包含任何对象)。所以集合已经知道包含的元素是UITouch。

Optionals

The first computed property does return an Optional. Infact if the Set is empty it does return nil.

第一个计算属性确实返回一个可选的。事实上,如果集合为空,它会返回nil。

A good and safe technique to unwrap the element is

一种很好的、安全的方法来展开元素

guard let touch = touches.first else {
    debugPrint("Ops, no touch found...")
    return
}

// here you can use touch