2

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. The gesture is recognized by my GameScene class in GameScene.swift. It then uses a struct named "variables" to send the "Score" to my GameViewController which will display the score via UILabel. This is were I need to convert the Int into a String. Simple. Except every method I've tried has ended with the error: fatal error: unexpectedly found nil while unwrapping an Optional value. Furthermore, whenever I try to treat "Score" as an optional (add a '?') XCode gives me another error and tells me to remove it.

TL;DR: I need to convert an Int into a String, and all the methods that usually work aren't.

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. 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; both return the correct value.

7
  • first question: why not use an sklabelnode instead of uilabel? Commented Jul 7, 2015 at 3:40
  • What you have tried should work, so clearly your problem is not in the Int to String conversion. Try printing the value of your variables struct after every statement once it is created or modified, and print it's value just before being used. This will help you see what is going on tremendously. If I had to guess, I would say that you just aren't initializing variables properly. Commented Jul 7, 2015 at 3:40
  • Also consider using NSNumberFormatter. Commented Jul 7, 2015 at 3:40
  • @AbdulAhmad "I don't know what I don't know", I'll go look into it. Thanks! Commented Jul 7, 2015 at 3:43
  • @Aderis If you mean logging it before I transfer it, and after it's been received, then I have. Commented Jul 7, 2015 at 3:44

4 Answers 4

1

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.

var Score : Int = 0

Finally, to display the score, you should use an SKLabelNode.

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

Then, in your 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)
}
Sign up to request clarification or add additional context in comments.

Comments

1

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. 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.


To any future googlers with the same problem:

  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)

Comments

1

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)

1 Comment

Thanks for the advice! Although I did init score four different ways per method (see question) including: var score: Int?, var score: Int!, var score: Int = 0, and var score = 0. Probably should've mentioned this in the question.
0

Try this :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.