So there are a lot of questions on here about how to convert JSON to an NSDictionary, but nothing I’ve found on how to stop it from happening in the first place.
I’m trying to develop an app. Part of the app, at least for now, is a WebView. In that WebView a GeoJSONFeature, specifically a polygon, is created. I then pass that JSON object to swift via webkit.messageHandlers. So this is in the webview/webpage:
function sendPath(path){
window.webkit.messageHandlers.fieldDataPass.postMessage(path);
}
the path object here will look something like this:
{
"type": "Feature",
"bbox": [-10.0, -10.0, 10.0, 10.0],
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-10.0, -10.0],
[10.0, -10.0],
[10.0, 10.0],
[-10.0, -10.0]
]
]
}
//...
}
So as I said, I send that through webkit.messageHandlers. The receive on the swift side is on my WKScriptMessageHandler:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "fieldDataPass" {
if let messageBody = message.body as? NSDictionary {
print("fieldDataPass: " + String(describing: message.body))
showNamePrompt(pathData: messageBody)
}
}
}
Nothing I do here will downcast it to a string, or to anything else. It comes through as an NSDictionary.
I just want to put it in a MKGeoJSONFeature but no type of conversion will make that happen. Why is it auto-converting to a NSDictionary? How can I stop that from happening? OR how can I easily convert from NSDictionary to MKGeoJSONFeature (has to be flexible to handle many different feature types). Please help.