as an exercise for coding, I wrote a small program to receive MySql data on the Internet on the iPhone. On the server side. I wrote a php script to return script json data.
In xcode i have
[code]
.
.
.
let jsonString = try? JSONSerialization.jsonObject(with: data!, options: [])
print(jsonString!)
.
.
.
[/code]
In the xcode console, I have the following:
[code]
(
{
Address = "1 Infinite Loop Cupertino, CA";
Latitude = "37.331741";
Longitude = "-122";
Name = Apple;
}
)
[/code]
I have a function
[code]
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
[/code]
When I pass jsonString to convertToDictionary (text :)
[code]
let dict = convertToDictionary(text: jsonString as! String)
[/code]
In the console, I get the error "I can not distinguish a value of the type" __NSSingleObjectArrayI "(0x10369bdb0) to" NSString "(0x1004eac60)."
but if I hardcode the json string, pass it to convertToDictionary (text :)
[code]
let hardCodedStr = "{\"Address\":\"1 Infinite Loop Cupertino, CA\",\"Latitude\":\"37.331741\",\"Longitude\":\"-122\",\"Name\":\"Apple\"}"
let dict = convertToDictionary(text: hardCodedStr)
print(dict!)
[/code]
It works great. Why is this? Thanks
source
share