0

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?

John Hubler
  • 877
  • 1
  • 11
  • 27
  • "it gives me a different error" Yes? And what is that different error? — You understand that JSON knows nothing of your LogInfo struct, so why are you thinking that an array of LogInfo objects is just going to turn magically into something that JSON can deal with? – matt Sep 24 '16 at 19:52
  • If I try it without the error: nil, it gives "Argument type [LogInfo] does not conform to the expected type 'AnyObject'' – John Hubler Sep 24 '16 at 19:53

0 Answers0