Tinderlike retrieve Webservice data in card

101 views Asked by At

i'm currently developping an app like tinder, which retrieve some datas from a webservice.

I already coded my function to retrieve datas which work like a charm, and i have also implemented the cardmodel.

The problem is, i don't know how to code the fact to put my decoding json data in my model.

My Code :

//Datas i send in post to my werbservice to retrieve datas

var post:NSString = "recherche=\(recherche)&category=\(category)&zipCode=\(zipCode)&price=\(price)"

var url:NSURL = NSURL(string:"urlofmywebservice")!

Response from the webservice :

[
  {
    "id": "1",
    "title": "bmw",
    "price": "500.00",
    "description": "330",
    "addedDate": "2015-05-18 00:00:00",
    "user_id": "1",
    "user_name": "CANOVAS",
    "user_zipCode": "32767",
    "category_id": "1",
    "category_label": "VEHICULES",
    "subcategory_id": "2",
    "subcategory_label": "Motos",
    "pictures":"http://cars.axlegeeks.com/sites/default/files/4315/media/images/2014_BMW_Z4_sDrive28i_3790993.jpg"
    "bdd": {}
  }
]

and if i have some datas :

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary


            let success:NSInteger = jsonData.valueForKey("success") as! NSInteger


            //[jsonData[@"success"] integerValue];

            NSLog("Success: %ld", success);

            if(success == 1)
            {

I call my cards in which i want to put decoding datas from my webservice:

let newCard = Model()
                newCard.image = UIImage(data: pictures)
                newCard.content = jsonData.valueForKey("content") as! NSstring
                newCard.desc = jsonData.valueForKey("desc") as! NSstring
                self.data.append(newCard)
                NSLog("fetch new data")

i already tried some things like "jsonData.valueForKey("desc") as! NSstring", but i got breackpoint.

Have you got an idea ?

1

There are 1 answers

1
tmato On

I recommend you to SwiftyJSON and Alamofire for HTML request and JSON decoding : https://github.com/Alamofire/Alamofire + https://github.com/SwiftyJSON/SwiftyJSON

They are really user-friendly.

You can then do something like this :

    Alamofire.request(.GET, url).responseJSON {
     (_, _, data, _) in
              var json = JSON(data!)
              var text = json["text"].string!
     }