In objective-c I can declare a function with an output argument, I suppose that is so called pass-by-reference:
-(void) doTask(int id, output: (NSArray**)results) {
…
results = getResult()
}
Then, I can access the results
to check the output of doTask()
outside the function scope.
In swift, is it still valid?
func doTask(id:Int, results:NSArray) -> Void {
…
results = getResult()
}
Can I pass the end results of doTask()
to the function argument results
and access it outside the function scope?
In Swift, you need to mark a parameter as being inout
; that is, you can write to it in the function and the changes to the variable take affect outside the function scope:
func doTask(id: Int, inout results: NSArray) -> Void {
// …
results = NSArray()
}
Then to call the function, you need to pass a valid variable of that type, and append it with an ampersand:
var results: NSArray()
doTask(4, &results)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments