post request can’t return an NSDictionary in swift

I am making a POST request in swift, and my code cannot collect the data as an NSDictionary.

let myUrl = NSURL(string: "http://www.mindyour.money/individual_players_by_sport_history.php");
    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let postString = "token=(token)&sport=(sports)";

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil
        {
            println("error=(error)")
            return
        }


        var err: NSError?

        var myJSON = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: nil) as? NSDictionary

        var json : JSON = JSON(data: data)

However, if I cast as NSArray this is easily picked up. I have successfully managed to collect as an NSDictionary before, but only from an NSData object. Could somebody help me?

An example of the son file should be something like this;

[

{
“gametype” : “Football FantasyLeagueGame”,
“player_username” : “BarryManilow”,
“event” : “Rep. of Ireland v England – Coupon”,
“stake” : “5.00”,
“seat_created” : “2015-06-07 12:14:51”,
“game_end_time” : “2015-06-07 14:50:00”,
“numseats” : “1”
}]

I use several of these key values to create a class array, later in the code, and I may have more questions later. Apologies.


Source: ios

Leave a Reply