Here I am getting a number from web service which I had stroed it in userdefaults and retrieving from user defaults and in another class and converting from json string to string to use it but unable to implement it and it got crashed at first line can anyone help help me how to resolve it ?
here is my code for retrieving
let customerId = UserDefaults.standard.value(forKey: "CustomerLoginNumber")
jsonToString(json: customerId as! String)
print("\(UserDefaults.standard.value(forKey: "CustomerLoginNumber")!)")
func jsonToString(json: String){
do
{
let data1 = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data
let convertedString = String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string
print(convertedString ?? "defaultvalue")
self.customerCartId = convertedString
} catch let myJSONError {
print(myJSONError)
}
}
and here is my error showing in line
2017-10-23 09:53:26.896 Ecommerce[966:20945] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'