I've been following the Microsoft Azure documentation on querying tables successfully (inserting, reading, and updating items into the database work fine), but at the end of a simple method, right off the docs:
func getAllEventIDs() -> [String] {
var events:[String] = [] //this is to be updated
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
let client = delegate.client! //boiler-plate for azure
let itemTable = client.tableWithName("Events")
itemTable.query().readWithCompletion {
//something about this query block might be preventing successful initialization to the events array
(result:MSQueryResult!, error) in
//usually error-checking here
for item in result.items {
events.append(item.valueForKey("id") as! String)
//returning events here...
}
//...and here (almost) work, since Swift expects a return of type Void
}
return events //still empty
}
I may not pass the array in as a parameter, since the .append
function will mutate that array.
The returned value is nothing but the initial empty array. However, this issue seems to stem heavily from Azure's query code block and not from Swift itself.
A simpler example:
func returnValueArray() -> [Int] {
var array:[Int] = [0,0,0,0]
var num:Int = 3
for var n = 0; n < array.count; n++ {
array[n] = num
}
return array
}
This returns [3,3,3,3]. Again, not Swift's problem, but Swift may have manifested Azure's return issue.
How can I return the desired, updated array at the end of query method? Is it possible to pass in a mutable array into the method, append values, and then return that array?