‘CGFloat’ is not convertible to ‘UInt8' and other CGFloat issues with Swift and Xcode 6 beta 4

前端 未结 4 1231
孤街浪徒
孤街浪徒 2020-12-11 02:09

In case this illuminates the problem, here\'s the original Objective-C code.

int x = (arc4random()%(int)(self.gameView.bounds.size.width*5)) - (int)self.game         


        
4条回答
  •  自闭症患者
    2020-12-11 02:17

    Since Swift doesn't have implicit type conversions, you must specify all the type conversions that take place. What makes this case particularly tedious, is that currently Swift seems to lack direct conversions between CGFloat and types such as UInt32, and you must go through an intermediate type as you've discovered.

    In the end, two double conversions are needed for the arc4random_uniform:

    let bounds = CGRectMake(0.0, 0.0, 500.0, 500.0)
    var x = CGFloat(UInt(arc4random_uniform(UInt32(UInt(bounds.size.width) * 5))))
    x -= bounds.size.width * 2
    let center = CGPointMake(x, -bounds.size.height)
    

提交回复
热议问题