0

I have an issue with my func in swift to get the data (JSON) back. I know its the completion handling but I am struggling so maybe someone can help.

The code below shows my function. The API needs the json as string in a field. The request itself works > the print(post) shows the correct data. But I don't get it out that function. (I use Alamofire, SwiftyJSON, swift2.2 and xcode 7.3)

class func searchCities(jsonStr: String) -> JSON {

    let oEndpoint = "https://api.domain.com/api/1.0/"
    guard let url = NSURL(string: oEndpoint) else {
        print("error url")
        return nil
    }

    // let parameters : [String: String] = ["one":"some param", "two":"some other param"]
    var result: JSON = JSON("")

    Alamofire.upload(.POST,
                     url,
                     multipartFormData: { multipartFormData in
                        multipartFormData.appendBodyPart(data: jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"data")
                        // parameters if needed  
                        // multipartFormData.appendBodyPart(data: value, name: key)
                    },
                     encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .Success(let upload, _, _):
                            upload.responseJSON{ response in
                                guard response.result.error == nil else {
                                    print("error response")
                                    print(response.result.error!)
                                    return
                                }
                                if let value: AnyObject = response.result.value{
                                    let post = JSON(value)
                                    print(post)
                                    result = JSON(value)                                    }
                            }
                        case .Failure( _):
                            print("failure")
                            break
                        }
        }
    )

    //print("res: \(result)")
    return result

}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Armin Mandel
  • 117
  • 11

1 Answers1

4

So I think figured it out ...

class func searchFeeds(jsonStr: String, completion: (response: JSON) -> Void) {

    let oEndpoint = "https://api.domain.com/api/1.0/"
    guard let url = NSURL(string: oEndpoint) else {
        print("error url")
        completion(response: nil)
        return
    }

    Alamofire.upload(.POST,
                     url,
                     multipartFormData: { multipartFormData in
                        multipartFormData.appendBodyPart(data: jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"data")
                        // parameters if needed
                        // multipartFormData.appendBodyPart(data: value, name: key)
                    },
                    encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .Success(let upload, _, _):
                            upload.responseJSON{ response in
                                guard response.result.error == nil else {
                                    print("error response")
                                    print(response.result.error!)
                                    completion(response: nil)
                                    return
                                }
                                if let value: AnyObject = response.result.value{
                                    completion(response: JSON(value))
                                }
                            }
                        case .Failure( _):
                            print("failure")
                            break
                        }
        }
    )
}

And get the result with ...

    APIclass.searchFeeds(jsonString) {
        (response: JSON) in

            print(response)

        }

Not perfect but it works.

Armin Mandel
  • 117
  • 11