To convert NSNumber Into String Swift 2
let tempNumber = userInfoDict.valueForKey("designation") as! NSNumber
let str2 = tempNumber.stringValue
Accurate result
Since the numeric result is supposed to be an Int
anyway you can omit the NSNumber
detour.
let tempNumber = userInfoDict.valueForKey("designation") as! Int
let stringTemp = String(tempNumber)
Side note: Since userInfoDict
seems to be a dictionary, don't use the key-value coding method valueForKey:
unless you really need the special functionality.
Either use key subscripting (recommended) userInfoDict["designation"]
or objectForKey:
You'll have to format the NSNumber's doubleValue member to a string before outputting.
let tempNumber:NSNumber = 6.0
let s:String = String(format:"%f", tempNumber.doubleValue) //formats the string to accept double/float
print(s)
You can refer the following code to see how NSNumber can be converted into NSString.
let a = 10
let aString = String(a)
Its better to avoid complicated codes for simple stuffs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With