Reputation: 67
Hi i have a question about some code.
Okay, the problem is that i have some code that works in one function, but gives me an error in another function. The first code block is the function that it works in. BTW, it is only one line:
@IBAction func searchPhotosByPhraseButtonTouchUp(sender: UIButton) {
if (!searchText.text.isEmpty) {
// 2: Replace spaces with +
var escapedSearchText:String = searchText.text.stringByReplacingOccurrencesOfString(" ", withString: "+")
// 3: API Method arguments
let methodArguments = [
"method": METHOD_NAME,
"api_key": API_KEY,
"text": escapedSearchText,
"format": FORMAT,
"nojsoncallback": NO_JSON_CALLBACK,
"extras": EXTRAS,
"safe_search": SAFE_SEARCH
]
// This line is the problem, if i make it in this function there is no problems
let urlString = BASE_URL + encodeParameters(params: methodArguments)
// 4: Call the Flickr API with these arguments
getImageFromFlickrBySearch(methodArguments)
}
else {
self.imageInfoLbl.text = "You did not write anything in the textfield"
}
}
So as you can see, in the code block above all is fine, but if i do it like this:
func getImageFromFlickrBySearch(methodArguments: [String: AnyObject]) {
// 5: Initialize session and url
...
// Here it gives me the error:
// Binary operator '+' cannot be applied to two String operands
let urlString = self.BASE_URL + encodeParameters(params: methodArguments)
...
}
I get a error. I have removed the rest of the code from the second code block function for clarity.
I should probably say that BASE_URL is a constant.
The only difference of the functions, is that one is a @IBAction??
Upvotes: 2
Views: 10337
Reputation: 1994
I'm not all too familiar with Swift but what could be causing your error is the methodArguments: [String: AnyObject]
. In the function that works, Swift knows that all objects in the dictionary are String objects, but in the second function, their types are AnyObject
.
If all of the key/value pairs are actually strings, change your parameter to methodArguments: [String: String]
and see if that works.
Upvotes: 1