Swift Cannot construct string with argument type Int64

江枫思渺然 提交于 2019-12-11 14:05:58

问题


I am making a game that involves a Game Center leaderboard. I want to make a custom leaderboard UI rather than using the default interface.

I am trying to convert the values stored in a Game Center leaderboard into a string so that I may display them using an SKLabelNode. However, I get an error saying that:

Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'

I am accessing the Game Center scores using

leaderboard.scores[i].value

When I use the String(describing: ) method, my label node reads "optional(10)", with whatever the score is being inside the parenthesis. I am wondering how to cleanly convey the data store in Game Center into a number in string format.


回答1:


Try optional binding:

if let unwrapped = leaderboard.scores[i].value {
    let string = String(unwrapped)
    print(string)
}

Or use the guard statement if you want to use the unwrapped value in the rest of the scope:

guard let unwrapped = leaderboard.scores[i].value else {
    fatalError("Couldn't unwrap the score value")
}
let string = String(unwrapped)


来源:https://stackoverflow.com/questions/52573390/swift-cannot-construct-string-with-argument-type-int64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!