2016-09-25 76 views
0

我目前正在使用cocoapods 1.1.0.rc.2。我已經安裝了Alamofire吊艙。使用1.1.0.rc.2版本,它應該可以使用swift 3.0。 不過我想聲明以下枚舉聲明枚舉路由器Alamofire swift 3.0

enum Router: URLRequestConvertible { 
     static let baseURLString = "https://api.instagram.com" 
     static let clientID = "myID" 
     static let redirectURI = "http://www.example.com/" 
     static let clientSecret = "mySecret" 

     case PopularPhotos(String, String) 
     case requestOauthCode 

     static func requestAccessTokenURLStringAndParms(code: String) -> (URLString: String, Params: [String: AnyObject]) { 
      let params = ["client_id": Router.clientID, "client_secret": Router.clientSecret, "grant_type": "authorization_code", "redirect_uri": Router.redirectURI, "code": code] 
      let pathString = "/oauth/access_token" 
      let urlString = Instagram.Router.baseURLString + pathString 
      return (urlString, params as [String : AnyObject]) 
     } 

     // MARK: URLRequestConvertible 

     var URLRequest: NSMutableURLRequest { 
      let result: (path: String, parameters: [String: AnyObject]?) = { 
       switch self { 
       case .PopularPhotos (let userID, let accessToken): 
        let params = ["access_token": accessToken] 
        let pathString = "/v1/users/" + userID + "/media/recent" 
        return (pathString, params as [String : AnyObject]?) 

       case .requestOauthCode: 
        let pathString = "/oauth/authorize/?client_id=" + Router.clientID + "&redirect_uri=" + Router.redirectURI + "&response_type=code" 
        return (pathString, nil) 
       } 
      }() 

      let baseURL = NSURL(string: Router.baseURLString)! 
      let URLRequest = NSURLRequest(url: NSURL(string: result.path ,relativeTo:baseURL as URL)! as URL) 
      let encoding = Alamofire.ParameterEncoding.encode(baseURL as! ParameterEncoding) 
      //let encoding = Alamofire.ParameterEncoding.URL 
      return encoding.encode(URLRequest, parameters: result.parameters).0 
     } 
    } 

但是我建立這個時候出現以下錯誤。

Type 'Router' does not conform to protocol 'URLRequestConvertible' 

有沒有人知道我在這裏錯過了什麼?

+0

我得到同樣的問題。你有沒有找到任何解決方案。 謝謝 –

+0

@ravisendhav不,我還沒有找到任何東西,不幸的是 – NoSixties

+0

你可以在我的下面的文章中查看這個解決方案。 –

回答

1
enum Router: URLRequestConvertible { 

    static let baseURLString = "www.google.com" 


    case getToken([String: AnyObject]) 

    var method: HTTPMethod { 

     switch self { 
     case .getToken: 
      return .post 
     default: 
      break 

     } 
    } 

    var path: String { 
     switch self { 
     case .getToken: 
      return "token" 
     default: 
      break 
     } 
    } 


    func asURLRequest() throws -> URLRequest { 

     let url = Foundation.URL(string: baseURLString)! 

     var urlRequest = URLRequest(url: url.appendingPathComponent(path)) 
     urlRequest.httpMethod = method.rawValue 


     urlRequest.setValue("application/json", forHTTPHeaderField: "Content") 
     urlRequest.setValue("keep-alive", forHTTPHeaderField: "Connection") 

     switch self { 
     case .getToken(let parameters): 
      urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters) 
     default: 
      break 
     } 
     return urlRequest 

    } 
}