5

I am trying to migrate my codes from swift 2 version to swift 3 version. I could not migrate following code part and I did not find any solution for it. How can I do it?

// 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
            }
        }
    }
Jessica
  • 165
  • 3
  • 12

2 Answers2

6

Looks like a lot has changed in Swift 3. Try to change your code by taking reference as below code.

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
        }
    }
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
1

I'd change the name of this computed property to, say, request, to avoid clashing with the new type name, URLRequest. Coincidentally, this computed property should use a type of 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)
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for your help Rob. But it gives and error on var request = URLRequest(url: url) like Cannot call value of non-function type 'URLRequest' – Jessica Mar 01 '17 at 07:49
  • Did you change the name of this variable? It can be confused by the type `URLRequest` when you have a variable of the same name. – Rob Mar 01 '17 at 07:55
  • Yes I tried but not working problem caused by URLRequest(url: url) not variable name I think – Jessica Mar 01 '17 at 08:01
  • No, the problem is elsewhere, because this is how we use `URLRequest` in Swift 3. Either you have some clash in with `URLRequest` in your code or you didn't define `URL` properly. – Rob Mar 01 '17 at 08:05
  • Thanks for your help Rob I fixed problem. Only added Alamofire before URLRequest. – Jessica Mar 01 '17 at 08:14