The discussion of the removal of Var from a function parameter is fully documented within this submission on GitHub: Remove Var Parameters
In that document you will find that people often confuse var
parameters with inout
parameters. A var
parameter simply means that the parameter is mutable within the context of the function, while with an inout
parameter the value of the parameter at the point of return will be copied out of the function and into the caller's context.
The correct way to solve this problem is to remove var
from the parameter and introduce a local var
variable. At the top of the routine copy the parameter's value into that variable.
Have you tried to assign to a new var
public func getQuestionList(language: String) -> NSArray {
var lang = language
if self.data.count > 0 {
if (lang.isEmpty) {
lang = "NL"
}
return self.data.objectForKey("questionList" + lang) as! NSArray
}
return NSArray()
}
Just add this one line at the beginning of the function:
var language = language
and the rest of your code can stay unchanged, like this:
public func getQuestionList(language: String) -> NSArray {
var language = language
if self.data.count > 0 {
if (language.isEmpty) {
language = "NL"
}
return self.data.objectForKey("questionList" + language) as! NSArray
}
return NSArray()
}
A lot of people are suggesting an inout
parameter, but that's really not what they're designed for. Besides, it doesn't allow calling the function with a let
constant, nor with a string literal. Why don't you simply add the default value to the function signature?
public func getQuestionList(language language: String = "NL") -> NSArray {
if data.count > 0 {
return data.objectForKey("questionList" + language) as! NSArray
} else {
return NSArray()
}
}
Just make sure to not call getQuestionList
with the empty string in case you want the default language, but just leave out the parameter:
let list = getQuestionList() // uses the default "NL" language
public func getQuestionList(language: inout String) -> NSArray {
if self.data.count > 0 {
if (language.isEmpty) {
language = "NL"
}
return self.data.objectForKey("questionList" + language) as! NSArray
}
return NSArray()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With