Swift无法使用参数类型Int64构造字符串
我正在制作一个包含Game Center排行榜的游戏.我想制作一个自定义排行榜用户界面,而不是使用默认界面.
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.
我试图将存储在Game Center排行榜中的值转换为字符串,以便可以使用SKLabelNode显示它们.但是,我收到一条错误消息:
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:
无法为类型为'(Int64?)'的参数列表的类型为'String'的初始化程序
Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'
我正在使用
leaderboard.scores[i].value
当我使用String(describing: )
方法时,无论括号内的分数是多少,标签节点都将读取"optional(10)".我想知道如何将Game Center中的数据存储干净地转换为字符串格式的数字.
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.
尝试可选绑定:
if let unwrapped = leaderboard.scores[i].value {
let string = String(unwrapped)
print(string)
}
或者如果您想在其余范围中使用未包装的值,请使用guard语句:
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)