I have an [[String:Any]]
object populated like:
var result : [[String : Any]] = [[String : Any]]()
And I need convert it to Data
.
I'm just using:
JSONEncoder().encode(result)
To convert it.
But I get this error:
Generic parameter 'T' could not be inferred
Exist a simple way to convert a [[String:Any?]] object to
Data` ?
Any built-in data type can be converted into its string representation by the str() function. Built-in data type in python include:- int , float , complex , list , tuple , dict etc. Method 2 : Defining __str__() function for a user defined class to be converted to string representation.
Using Int initializer Swift provides the function of integer initializers using which we can convert a string into an Int type. To handle non-numeric strings, we can use nil coalescing using which the integer initializer returns an optional integer.
Drop in the Project Navigator or use XCode File->Add file to add the . json to your project. From that point on, you can edit that file directly.
It is a collection of bytes ( [UInt8] array of unsigned integer 8 bits 0-255).
JSONEncoder
can only encode objects whose type conforms to Encodable
. If you want to encode Any
to JSON, you need to use JSONSerialization
to do that.
let jsonData = try? JSONSerialization.data(withJSONObject:result)
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