加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Init已在Swift 3中重命名为init(描述)错误

发布时间:2020-12-14 05:31:33 所属栏目:百科 来源:网络整理
导读:这段代码在 Swift 2中工作正常: guard let userData = responseData["UserProfile"] as? [String : AnyObject] else { return }var userProfileFieldsDict = [String: String]()if let profileUsername = userData["Username"] as? NSString { userProfileF
这段代码在 Swift 2中工作正常:
guard let userData = responseData["UserProfile"] as? [String : AnyObject] else { return }

var userProfileFieldsDict = [String: String]()
if let profileUsername = userData["Username"] as? NSString {
  userProfileFieldsDict["username"] = String(profileUsername)
}
if let profileReputationpoints = userData["ReputationPoints"] as? NSNumber {
  userProfileFieldsDict["reputation"] = String(profileReputationpoints)
}

但是,在Swift 3中,它会在userProfileFieldsDict [“reputation”]上引发错误

init has been renamed to init(describing:)

我的问题是为什么它会触发该行而不是userProfileFieldsDict [“username”]赋值行,以及如何修复它?我假设它是因为我正在将一个NSNumber转换为String,但我无法理解为什么这很重要.

NSNumber是一个非常通用的类.它可以是从bool到long甚至是char的任何东西.所以编译器真的不确定确切的数据类型,因此它无法调用正确的String构造函数.

而是使用String(describe :)构造函数,如下所示

userProfileFieldsDict["reputation"] = String(describing: profileReputationpoints)

关于它,这里有更多info.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读