xcode Float 不可转换为 CGFloat 且 CGFloat 不可转换为 Float
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24951634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Float is not convertible to CGFloat and CGFloat is not convertible to Float
提问by Pedro.Alonso
I have a problem with swift on XCode 6 beta 4, is driving me nuts I'm in IOS 8 developing a game and I have follow a tutorial but I get this: Float is not convertible to CGFloat and then when I recast as CGFloat I get the other. Here is the code:
我在 XCode 6 beta 4 上的 swift 有问题,让我发疯了我在 IOS 8 开发游戏,我已经按照教程,但我得到了这个:Float 不能转换为 CGFloat 然后当我重新转换为 CGFloat 我得到另一个。这是代码:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//physics
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
// Bird
var birdTexture = SKTexture(imageNamed: "kirby")
birdTexture.filteringMode = SKTextureFilteringMode.Nearest
bird.setScale(0.5)
bird.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)
bird.physicsBody = SKPhysicsBody(circleOfRadius:bird.size.height/2)
bird.physicsBody.dynamic = true
bird.physicsBody.allowsRotation = false
self.addChild(bird)
//ground
ground.setScale(2.0)
ground.position = CGPointMake(self.size.width/2, ground.size.height/2)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width , ground.size.height))
ground.physicsBody.dynamic = false
self.addChild(ground)
//pipes
//create pipes
//^
//movement of the pipes
let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width)
let movePipe = SKAction.moveByX(-distanceToMove, y: CGFloat(0.0), duration: NSTimeInterval(0.01) * distanceToMove) <----- this line is the problem
}
What is going on, I move the line:
这是怎么回事,我移动了一行:
CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width)
I get the second error, then a recast:
我得到第二个错误,然后重铸:
Float(CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width))
or
或者
Float(self.frame.size.width + 2.0 * pipeUp.texture.size().width)
gives me the first, so what do swift wants here, that is insane. Catch 22? Any help?
给了我第一个,所以 swift 在这里想要什么,那太疯狂了。抓住22?有什么帮助吗?
EDIT: I'm using a 13-inch mac late 2009, Intel Core 2 Duo, on mavericks OS X 10.9.4 (13E28) if any help. I saw something about the building options may affect the float types, but I do not know where are they.
编辑:如果有帮助,我在 Mavericks OS X 10.9.4 (13E28) 上使用 2009 年末的 13 英寸 mac,Intel Core 2 Duo。我看到一些关于建筑选项可能会影响浮动类型的信息,但我不知道它们在哪里。
采纳答案by Markus Persson
This will be interpreted as a CGFloat
and not as a NSTimeInterval
as you cast and then multiply:
这将被解释为 aCGFloat
而不是 a ,NSTimeInterval
因为你投然后乘:
let movePipe = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01) * distanceToMove)
Try to multiply then cast to NSTimeInterval
:
尝试乘以然后投射到NSTimeInterval
:
let movePipe = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
Also, as suggested already, you don't need to cast your distance. The following will work just fine:
此外,正如已经建议的那样,您不需要投射距离。以下将正常工作:
let distanceToMove = self.frame.size.width + 2.0 * pipeUp.texture.size().width
回答by Sulthan
Just make sure every operand has the same type. Literals (2.0
) have always their type inferred. In your case, I see no problem because
只要确保每个操作数具有相同的类型。文字 ( 2.0
) 总是可以推断出它们的类型。在你的情况下,我认为没有问题,因为
let distanceToMove: CGFloat = self.frame.size.width + 2.0 * pipeUp.texture.size().width
has all the operands of type CGFloat
so there is no need to cast at all.
具有所有类型的操作数,CGFloat
因此根本不需要强制转换。
On the next line, again make sure to use the correct types. You don't need to cast the literals:
在下一行,再次确保使用正确的类型。您不需要强制转换文字:
SKAction.moveByX(-distanceToMove, y: 0.0, duration: 0.01 * NSTimeInterval(distanceToMove))