Alamofire and SwiftyJSON Error

1k views Asked by At

I have installed Alamofire and SwiftyJSON with Cocoapods. Independently, each are working great. However when I try and mix the two I get an error.

Before using Alamofire I used NSURLConnection to download data. When the connectionHandler finished I created a JSON object with SwiftyJSON.

        let json = JSON(data: self.downloadedData!)

This worked without any issues.

To make things cleaner I wanted to use Alamofire. When I use Alamofire I try to create a JSON object with SwiftyJSON.

Alamofire.request(.GET, "http://162.208.56.92/json_service.php").responseJSON()
            {
                (_, _, JSON, _) in

                let json = JSON(data: JSON)
        }

When I do this I get the following error.

Cannot invoke 'JSON' with an argument list of type '(data: AnyObject?)'

Since JSON takes an NSData argument I tried the following.

Alamofire.request(.GET, "http://162.208.56.92/json_service.php").responseJSON()
            {
                (_, _, JSON, _) in

                let json = JSON(data: JSON as! NSData)
        }

This gave me the following error.

Cannot invoke 'JSON' with an argument list of type '(data: NSData)'

I am stumped. Any thoughts?

3

There are 3 answers

1
Icaro On

You must encode the result using dataUsingEncoding. Try to convert your result using the code below:

    Alamofire.request(.GET, "http://162.209.75.59/json_service.php").responseJSON()
    {
        (_, _, responseBody, _) in
        // Convert the response to NSData to handle with SwiftyJSON
        if let data = (responseBody as NSString).dataUsingEncoding(NSUTF8StringEncoding) {
            let json = JSON(data: data)
            println(json)
        }
    }
0
jonthornham On

lcaro's answer sent me in the right direction but I had to make a few changes. The code below was lcaro's answer.

 Alamofire.request(.GET, "http://162.209.75.59/json_service.php").responseJSON()
    {
        (_, _, responseBody, _) in
        // Convert the response to NSData to handle with SwiftyJSON
        if let data = (responseBody as NSString).dataUsingEncoding(NSUTF8StringEncoding) {
            let json = JSON(data: data)
            println(json)
        }
    }

I changed the responseJON() to responseString() and then removed the NSString cast on responseBody.

 Alamofire.request(.GET, "http://162.209.75.59/json_service.php").responseString()
    {
        (_, _, responseBody, _) in
        // Convert the response to NSData to handle with SwiftyJSON
        if let data = (responseBody).dataUsingEncoding(NSUTF8StringEncoding) {
            let json = JSON(data: data)
            println(json)
        }
    }
0
Yeis Gallegos On

Hey there I'm using both Alamofire and SwiftyJSON to get the url from an image dictionary here how my code looks

var Photos = [String]() 
override func viewDidLoad() {
    super.viewDidLoad()
       Alamofire.request(.GET, "https://api.500px.com/v1/photos",parameters:["consumer_key":"gRU4LletUCA9RiOQhaJBAt62UyRRYUE6vsIcC7fO"])
         .responseJSON { _,_,result in
            switch result {
            case .Success(let data):
                let json = JSON(data)
                self.Photos = self.ParseJSON(json)
            case .Failure(_, let error):
                print("Request failed with error: \(error)")
            }
            debugPrint(self.Photos)
    }

Basically what is doing is making the request and if it is succesfull i save the data in a JSON object then i just parse the image_url in array called Photos

here also my ParseJSON fun

func ParseJSON(json:JSON)->[String]
{
    //Get Image_URL
    var pictures = [String]()
    for result in json["photos"].arrayValue{
        pictures.append(result["image_url"].stringValue)
    }
    return pictures
}

I'm currently using Almaofire 2.1 and the recent version of SwiftyJSON