alamofire objectmapper library 를 사용해서 파파고 번역 구현하기입니다.



//

//  PapagoDTO.swift

//  Papago

//

//  Created by stayfoolish on 2018. 9. 25..

//  Copyright © 2018년 stayfoolish. All rights reserved.

//


import UIKit

import ObjectMapper


class PapagoDTO: Mappable {

    

    var message : Message?

    

    required init?(map: Map) {

        

    }

    

    func mapping(map: Map) {

        message <- map["message"]

    }

    

    class Message : Mappable{

        

        var result : Result?

        

        required init?(map: Map) {

            

        }

        

        func mapping(map: Map) {

            result <- map["result"]

        }

        class Result : Mappable{

            

            var translatedText : String?

            

            required init?(map: Map) {

                

            }

            

            func mapping(map: Map) {

                translatedText <- map["translatedText"]

            }

            

            

        }

        

        

    }


}


//

//  ViewController.swift

//  Papago

//

//  Created by stayfoolish on 2018. 9. 25..

//  Copyright © 2018년 stayfoolish. All rights reserved.

//


import UIKit

import Alamofire

import AlamofireObjectMapper


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        

        var url = "https://openapi.naver.com/v1/papago/n2mt"

        var params = ["source":"ko",

                      "target":"en",

                      "text":"만나서 반갑습니다."]

        var header = ["Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",

                      "X-Naver-Client-Id":"Jx1o3o2eyOTv_odH_gNw",

                      "X-Naver-Client-Secret":"ewr_h8Gsgv"]

        

        Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default, headers: header).responseObject { (response:DataResponse<PapagoDTO>) in

            var papagoDTO = response.result.value

            print((papagoDTO?.message?.result?.translatedText)!)

        }

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}



+ Recent posts