As most of you will probably be able to tell, I am new to iOS programming and Swift. Ultimately, I am trying to take a compilation of data, create a JSON string out of it, and pass it to a PHP file. I have the passing to the PHP file working with simple data - but I am hitting a brick wall when it comes to converting this array into JSON.
I have the following code:
struct LogInfo {
var species: String
var diameter: Float
var formClass: Int
var numLogs: Float
var boardFootage: Double
}
var logInfoArray = [LogInfo]()
logInfoArray.append(LogInfo(species: "Cypress", diameter: 18.0, formClass: 78, numLogs: 1.5, boardFootage: 100.0))
logInfoArray.append(LogInfo(species: "Spruce", diameter: 18.0, formClass: 78, numLogs: 1.5, boardFootage: 100.0))
logInfoArray.append(LogInfo(species: "Red Oak", diameter: 18.0, formClass: 78, numLogs: 1.5, boardFootage: 100.0))
From here, I want to take the data in logInfoArray, and parse it into JSON.
I tried this:
let data = NSJSONSerialization.dataWithJSONObject(logInfoArray, options: nil, error: nil)
let string = NSString(data: data!, encoding: NSUTF8StringEncoding)
But that throws an error saying that there's an extra argument in the call. When I remove the "error: nil" piece, it gives me a different error.
Can anybody help me convert this array of struct data into JSON?