I've been writing swift/php/sql for 3 months now... (I know I'm a rookie). I can create simple POST strings and issue http requests... etc.
Now I'm trying to push multiple objects to my server to add to my database. So I'm thinking the best way to transport the objects is to use a big json string. Maybe not... idk. I have been unsuccessful at creating the multiple object json string to post. Ideally, I'd like a string that PHP json_decode($myJsonString) will decode directly... I've created a playground to use as a test case. The call to JSON(myPlaceArray) results in "unknown".
Suggestions on how fix this or and easier way welcome! thank you!
import UIKit
import SwiftyJSON
class myClass {
public struct Place {
var PlaceId = String()
var name = String()
var address = String()
var latitude = Double()
var longitude = Double()
}
}
var myPlaceArray = [myClass.Place()]
var myPlace = myClass.Place()
for i in 0..<3 {
myPlace.PlaceId = "\(i)"
myPlace.address = "My Address \(i)"
myPlace.name = "My Name \(i)"
myPlace.latitude = 37.123
myPlace.longitude = -127.321
if(i==0){
myPlaceArray[0] = myPlace
}
else {
myPlaceArray.append(myPlace)
}
}
print(myPlaceArray[0])
let json = JSON(myPlaceArray)
print(json) // results: "unknown"
let jsonMyPlaceArray = JSON(myPlaceArray)
print(jsonMyPlaceArray)
//setup json post data
//send HTTP POST
let myUrl = NSURL(string: "https://myUrl.php")
let request = NSMutableURLRequest(url: myUrl! as URL)
request.httpMethod = "POST"
let postString = "jsonPlaces=\(String(describing: jsonMyPlaceArray))"
print(postString) // jsonPlaces=unknown
//.... make request