简体   繁体   English

Swift无法使用常规方法将Int转换为String

[英]Swift Cannot convert Int to String with usual methods

The general purpose of converting an Int into a String is to display a "Score", which is incremented by one, every time a SKSpriteNode is tapped. 将Int转换为String的一般目的是在每次点击SKSpriteNode时显示“Score”,该值增加1。 The gesture is recognized by my GameScene class in GameScene.swift. 该手势由GameScene.swift中的GameScene类识别。 It then uses a struct named "variables" to send the "Score" to my GameViewController which will display the score via UILabel. 然后它使用一个名为“variables”的结构将“Score”发送到我的GameViewController,它将通过UILabel显示得分。 This is were I need to convert the Int into a String. 这是我需要将Int转换为String。 Simple. 简单。 Except every method I've tried has ended with the error: fatal error: unexpectedly found nil while unwrapping an Optional value . 除了我尝试过的每个方法都以错误结束: 致命错误:在展开Optional值时意外发现nil Furthermore, whenever I try to treat "Score" as an optional (add a '?') XCode gives me another error and tells me to remove it. 此外,每当我尝试将“Score”视为可选项(添加'?')时,XCode会给我另一个错误并告诉我将其删除。

TL;DR : I need to convert an Int into a String, and all the methods that usually work aren't. TL; DR :我需要将Int转换为String,而通常工作的所有方法都不是。

Method's I've tried: 方法我尝试过:

  1. scoreLabel.text = NSNumber(integer: Score).stringValue
  2. scoreLabel.text = "\\(Score)"
  3. scoreLabel.text = String(Score)
  4. scoreLabel.text = toString(Score)
  5. scoreLabel.text = "\\(Score.description)"
  6. scoreLabel.text = Score.description
  7. scoreLabel.text = "\\(NSNumber(integer: Score).stringValue)"

I've also restarted XCode and Mac. 我还重启了XCode和Mac。 Have I missed something obvious? 我错过了明显的东西吗? Please help a noob out. 请帮助一个菜鸟。

EDIT #1: I forgot to mention that I've been logging "Score" in both GameScene.swift and GameViewController.swift; 编辑#1:我忘了提到我一直在GameScene.swift和GameViewController.swift中记录“得分”; both return the correct value. 两者都返回正确的值。

As Abdul Ahmad suggested, I should use a SKLabelNode rather than a UILabel to display my score because I'm doing so from a subclass of SKScene. 正如Abdul Ahmad建议的那样,我应该使用SKLabelNode而不是UILabel来显示我的分数,因为我是从SKScene的子类中这样做的。 I don't know why the value couldn't be converted in my other class (as Aderis pointed out, all my methods should've worked), as I was able to log it using the same syntax and methods I was using to attempt to set the UILabel's text. 我不知道为什么这个值无法在我的其他类中转换(正如Aderis指出的那样,我的所有方法都应该工作),因为我能够使用我用来尝试的相同语法和方法来记录它设置UILabel的文本。


To any future googlers with the same problem: 对于任何未来具有相同问题的google者:

  1. Check your syntax 检查你的语法
  2. Log the value at every point it could change 在可能发生变化的每个点记录值
  3. Try to use similar classes? 尝试使用类似的类? (I used a SKScene to change the text of a SKLabelNode rather than a UILabel) (我使用SKScene来更改SKLabelNode而不是UILabel的文本)

Problem is swift checking type. 问题是快速检查类型。 You declare an optional value and when you use it. 您声明一个可选值并在使用它时。 You need force it not nil. 你需要强迫它不是零。

// declare optional variable
var score: Int?

// Use it when you sure not nil
yourLabel.text = String(score!)

I advice you need to set initial value. 我建议你需要设置初始值。

var score: Int = 0

And use it. 并使用它。

yourLabel.text = String(score)

The error you are receiving isn't because of the proper code, its because your "Score" variable is nil. 您收到的错误不是因为正确的代码,而是因为您的“得分”变量为零。 What I always use is -> 我一直用的是 - >

scoreLabel.text = "\(Score)"

Then, set your the value of your "Score" to 0 so its value can't be returned nil. 然后,将“得分”的值设置为0,这样它的值就不能返回为零。

var Score : Int = 0

Finally, to display the score, you should use an SKLabelNode. 最后,要显示分数,您应该使用SKLabelNode。

var scoreLabel = SKLabelNode(fontNamed: "American Typewriter")

Then, in your didMove(toView) 然后,在你的didMove(toView)

override func didMove(to view: SKView) {
    scoreLabel.text = "\(Score)"
    scoreLabel.fontColor = UIColor.green
    scoreLabel.fontSize = 30
    scoreLabel.position = CGPoint(x: 0, y: self.frame.height / 4)
    self.addChild(scoreLabel)
}

Try this : 尝试这个 :

    let cast_this_optional_integer = Int(Score!)
    print(String(cast_this_optional_integer))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM