3

I have a number that I want to represent the player's score in a SpriteKit game I'm developing.

var score = 000000000

I then create an SKLabelNode with that as part of the string ("SCORE: \(score)") . So when I run it, it should currently say: "SCORE: 000000000".

However, it actually says: "SCORE: 0".

I'm making a 2D-platformer in the spirit of the classic games, so I really want the score to be formatted in this way. I see some solutions for this in JavaScript, but nothing in Swift.

RedEagle2000
  • 226
  • 2
  • 18

1 Answers1

7

The literal 000000000 means the same thing as 0 to the compiler.

You can use stringWithFormat: to add leading zeros when converting to a string (assuming you have import Foundation):

String(format: "%09ld", score)
jtbandes
  • 115,675
  • 35
  • 233
  • 266