Search code examples
swiftnsurlrequestalamofire

String is not convertible to String:AnyObject


I am trying out the Alamofire helpers for networking with my server. I am building up a router for handling my API endpoints. The construction itself seems clear to me, but I am struggling with some SWIFT syntax.

enum Router:URLRequestConvertible {
    static let baseURLString = "url"

    case AEDS

    var URLRequest: NSURLRequest {
        let (path: String, parameters: [String: AnyObject]) = {
            switch self {
            case .AEDS:
                let params = [""]
                return("/aeds", params)
            }
        }()

        let URL = NSURL(string: Router.baseURLString)
        let URLRequest = NSURLRequest(URL: URL!.URLByAppendingPathComponent(path))
        let encoding = Alamofire.ParameterEncoding.URL
        return encoding.encode(URLRequest, parameters: parameters).0
    }

}

I get the message that inside my case .AEDs the params are throwing an error: [String] is not convertible to [String: AnyObject]

I am kind of new to Swift and could not figure out so far, where to start. I think I provided the array that I am defining. So what does this error mean?


Solution

  • In your switch case, you need to defines params as a dictionary and not as an array.

     switch self {
            case .AEDS:
                let params = [""]  <---- This is initialising an array containing a string
                return("/aeds", params)
            }
    

    Try changing to:

    switch self {
                case .AEDS:
                    let params = ["" : ""]  <---- This will create a dict
                    return("/aeds", params)
                }
    

    That should solve your problem.