Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Rea Rahhal
PLUS
Rea Rahhal
Courses Plus Student 4,732 Points

I am using swift 4and getting the following errors although my code is the same : extraneous argument label 'contentsOf

/AND Cannot convert return expression of type '[(String, String)]' to return type '[(String)]'

import Foundation extension String{ func addingPercentEncoding() ->String{

    return self.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
}

} func encodeParametersInUrl(_ parameters:[String:Any])->String{

var components = [(String,String)]()
let sortedKeys = parameters.keys.sorted(by:{ $0 < $1 })

for key in sortedKeys{
    let value = parameters[key]
    let queryComponents = queryComponentsWith( key: key, value: value)
    components.append(contentsOf: queryComponents)

}
let encodedComponents = components.map{"\($0)=\($1)"}/
return encodedComponents.joined(separator: "&")

}

func queryComponentsWith(key:String, value:Any) -> [(String: String)]{ var components = (String,String) if let dictionary = value as? [String: Any]{ for(nestedKey,value) in dictionary{

        let nestedComponents = queryComponentsWith(key: "\(key)[\(nestedKey)]", value: value)//outer key
        components.append(contentsOf: nestedComponents)
    }
}else if let array = value as? [Any]{
    for value in array{
        let nestedComponents = queryComponentsWith(key: "\(key)[]", value: value)
        components.append(contentsOf: nestedComponents)
    }
}else{
    let encodedKey = key.addingPercentEncoding()
    let encodedValue = "\(value)".addingPercentEncoding()

    let component = (encodedKey,encodedValue)
    components.append(component)
}
return components

} let parameters = ["foo" : true] encodeParametersInUrl(parameters)