Swift无法使用参数类型Int64构造字符串 [英] Swift Cannot construct string with argument type Int64

查看:275
本文介绍了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)

这篇关于Swift无法使用参数类型Int64构造字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆