0

I am gathering an NSNumber from my plist and I want to print that in a String:

let tijd = String(self.sortedHighscores[indexPath.row]["Tijd"]!)
cell.detailTextLabel?.text = "\(tijd) seconden"

But in Simulator I'm seeing printed: Optional(120) seconden for example. How do I solve this? I've tried unwrapping tijd, but that would result in errors in the compiler telling me to delete the !.

jbehrens94
  • 2,356
  • 6
  • 31
  • 59
  • first yo have to unwrap then convert into string this is a duplicate of http://stackoverflow.com/questions/24161336/convert-int-to-string-in-swift – ogres Jul 08 '15 at 12:46

2 Answers2

0

Try this:

 var highscore : NSNumber =  self.sortedHighscores[indexPath.row]["Tijd"] as! NSNumber
 var a : String = String(format:"%d",highscore.integerValue)
 cell.detailTextLabel.text = a
iAnurag
  • 9,286
  • 3
  • 31
  • 48
-1

tijd being an optional, you need to unwrap it, but you should check whether it has a value, too.

if let foo = tijd {
    cell.detailTextLabel?.text = "\(foo) seconden"
}

If you know it will always have a value, "\(tijd!) seconden" should work also.

Jeremy
  • 4,339
  • 2
  • 18
  • 12