Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSNumber into String Swift 2 [closed]

Tags:

ios9

swift

To convert NSNumber Into String Swift 2

let tempNumber = userInfoDict.valueForKey("designation") as! NSNumber
  let str2 = tempNumber.stringValue

Accurate result

like image 350
Maninderjit Singh Avatar asked Dec 22 '15 14:12

Maninderjit Singh


3 Answers

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:

like image 107
vadian Avatar answered Oct 19 '22 17:10

vadian


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)
like image 40
CTABUYO Avatar answered Oct 19 '22 17:10

CTABUYO


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.

like image 4
Abhishek Avatar answered Oct 19 '22 19:10

Abhishek