2016-09-04 12 views
1

このコードを使用しようとしているときに、私はエラーを取得しています:ここでカスタムヘッダ - Alamofire +スウィフト3

func getRawJSON(method: String, paramether: String) { 
    let publicKey = "publicKeyHere" 
    let secretKey = "secretKeyHere 
    let APIURL = "https://www.bitmarket.pl/api2/" 
    let time = String(Int(NSDate().timeIntervalSince1970)) 

    let query = NSURLComponents() 
    query.queryItems = [NSURLQueryItem(name: "method", value: method) as URLQueryItem, 
         NSURLQueryItem(name: "tonce", value: time) as URLQueryItem] 

    let requestString = query.query! 
    let requestData = Array(requestString.utf8) 

    let params = [ 
     "method": method, 
     "tonce:": time 
     ] 

    let hmac: Array<UInt8> = try! HMAC(key: secretKey.utf8.map({$0}), variant: .sha512).authenticate(requestData) 

    let hmacString = hmac.map{String(format: "%02X", $0)}.joined(separator: "").lowercased() 

    let URL = NSURL(string: APIURL)! 
    let mutableURLRequest = NSMutableURLRequest(url: URL as URL) 
    mutableURLRequest.httpMethod = "POST" 

    do { 
     mutableURLRequest.httpBody = try JSONSerialization.data(withJSONObject: paramether, options: JSONSerialization.WritingOptions()) 
    } catch { 
    } 

    mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") 
    mutableURLRequest.setValue(publicKey, forHTTPHeaderField: "API-Key") 
    mutableURLRequest.setValue(hmacString, forHTTPHeaderField: "API-Hash") 

    Alamofire.request(mutableURLRequest) //Here is a problem 

} 

がエラーである:私は間違っ

Argument type 'NSMutableURLRequest' does not conform to expected type 'URLRequestConvertible' 

何をしているのですか? Alamofireのドキュメントによると、NSMutableURLRequestはURLRequestConvertibleに準拠する可能性があります。

答えて

2

スイフト3は、プロトコルURLRequestConvertibleに準拠したURLRequestを定義しています。 NSMutableURLRequestの代わりにURLRequestを使用する必要があります。

discussion.