Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSNumber to String in Swift4?

Tags:

How to convert an Array of NSNumber to Array of String in order to display the value in UITableView?

cell.textlabel.text = ?

Code:

var a = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001, 
         61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001, 
         0.8501030000000001, 3.647296, 1.28503]
like image 963
Dhanush Kumar Sivaji Avatar asked May 23 '18 04:05

Dhanush Kumar Sivaji


People also ask

What is NSNumber?

Overview. NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .

How to convert string value to Float in Swift?

var stringToInt: Int = Int (string)! This is a swift program where we are accepting a variable value as an integer data type and converting that integer variable value to a float data type and displaying the value and type using print statements.

How to convert string into Double iOS Swift?

Convert Swift String to DoubleUse Double , Float , CGFloat to convert floating-point values (real numbers). let lessPrecisePI = Float("3.14") let morePrecisePI = Double("3.1415926536") let width = CGFloat(Double("200.0")!)

How to convert string to Double in iOS?

Converting string to double To convert a string to a double, we can use the built-in Double() initializer syntax in Swift. The Double() initializer takes the string as an input and returns the double instance.


2 Answers

just add

.stringValue

to your NSNumber variable

like image 93
Aviram Netanel Avatar answered Oct 21 '22 07:10

Aviram Netanel


From what you posted is an array of Double if you don't annotate them explicitly. If the array you posted is as it is, then you need this:

let arrayOfDoubles = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001, 61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001, 0.8501030000000001, 3.647296, 1.28503]
let stringArrayOfDoubles = arrayOfDoubles.map { String($0) }

Or, if you explicitly annotate the type as [NSNumber] then you will need this:

let arrayOfNumbers: [NSNumber] = [68.208983, 6.373902, 1.34085, 3.974012, 110.484001, 61.380001, 1.325202, 0.8501030000000001, 0.8501030000000001, 0.8501030000000001, 3.647296, 1.28503]
let stringArrayOfNumbers = arrayOfNumbers.map { $0.stringValue }
like image 26
nayem Avatar answered Oct 21 '22 08:10

nayem