2017-03-01 70 views
3

我試圖將我的代碼從swift 2版本遷移到swift 3版本。 我無法遷移下面的代碼部分,我沒有找到任何解決方案。 我該怎麼辦?Alamofire類型'ParameterEncoding'沒有成員'URL'Swift 3

// MARK:URLRequestConvertible

 public var URLRequest: NSMutableURLRequest { 
      let urlComponent = NSURLComponents(string: Router.baseURL)! 
      urlComponent.path = Router.basePath.stringByAppendingString(path) 

      let mutableURLRequest = NSMutableURLRequest(URL: urlComponent.URL!) 
      mutableURLRequest.HTTPMethod = method.rawValue 

      var parameters: [String: AnyObject] = Dictionary() 
      parameters["key"] = Router.key 
      parameters["hl"] = "en" 

      switch self { 
      case .getMostPopularVideos(let pageToken): 
       parameters["part"] = "snippet,contentDetails,statistics" 
       parameters["chart"] = "mostPopular" 
       parameters["videoCategoryId"] = TubeTrends.Settings.topTrendsCat 
       if let pageToken = pageToken { 
         parameters["pageToken"] = pageToken 
       } 
       return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0 //This part Giving Error like Alamofire Type 'ParameterEncoding' has no member 'URL' 

//   default: 
//    return mutableURLRequest 
      } 
     } 
    } 
+0

我假設你不僅更新到斯威夫特3,而且還採用了最新版的Alamofire的? – Rob

+0

是的你是正確的 – Jessica

回答

1

我會改變這個計算的性質,比方說,request的名稱,以避免與新的類型名稱,URLRequest衝突。巧合的是,這個計算的性質應該使用類型的URLRequest

public var request: URLRequest { 
    let url = URL(string: Router.baseURL)! 
     .appendingPathComponent(Router.basePath) 
     .appendingPathComponent(path) 

    var request = URLRequest(url: url) 
    request.httpMethod = method.rawValue 

    var parameters = [String: Any]() 
    parameters["key"] = Router.key 
    parameters["hl"] = "en" 

    switch self { 
    case .getMostPopularVideos(let pageToken): 
     parameters["part"] = "snippet,contentDetails,statistics" 
     parameters["chart"] = "mostPopular" 
     parameters["videoCategoryId"] = TubeTrends.Settings.topTrendsCat 
     if let pageToken = pageToken { 
      parameters["pageToken"] = pageToken 
     } 
     return try! Alamofire.URLEncoding.default.encode(request, with: parameters) 
    } 
} 
+0

感謝您的幫助羅布。但它給和var request = URLRequest(url:url)like錯誤無法調用非函數類型的值'URLRequest' – Jessica

+0

您是否更改此變量的名稱?當你有一個相同名字的變量時,它可能被'URLRequest'類型混淆。 – Rob

+0

是的我試過但沒有工作的問題引起的URLRequest(url:url)不是變量名我認爲 – Jessica

5

貌似很多雨燕3.嘗試改變,採取參考如下代碼來改變你的代碼。

func asURLRequest() throws -> URLRequest { 
     let url = URL(string: Router.baseURLString)! 
     var urlRequest = URLRequest(url: url.appendingPathComponent(path)) 
     urlRequest.httpMethod = method.rawValue 

     if let token = Router.OAuthToken { 
      urlRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") 
     } 

     switch self { 
     case .createUser(let parameters): 
      return try Alamofire.JSONEncoding.default.encode(urlRequest, with: parameters) 
     case .updateUser(_, let parameters): 
      return try Alamofire.URLEncoding.default.encode(urlRequest, with: parameters) 
     default: 
      return urlRequest 
     } 
    } 
相關問題