Suppressing Implicit Returns in Swift

How to Supress implicit return from a closure with Void return type in Swift

I recreated your example with your code (with minor tweaks) and didn't have the problem you described. I used a swift dictionary instead though, since I have no knowledge about Obj-C.

func doSomething(completionHandler: Bool -> Void) {
completionHandler(true)
}

doSomething() { finished in
var data = [String: String]()
data.updateValue("data1", forKey: "data1") // 1
data.updateValue("data2", forKey: "data2") // 2
data.updateValue("data3", forKey: "data3") // 3

for (key, value) in data {
println("Key: \(key) & Value: \(value)")
}
}

The output is:

Key: data2 & Value: data2
Key: data1 & Value: data1 // Not sure why data1 is second here
Key: data3 & Value: data3

I doubt using NSDictionary could be the cause of it, maybe something else is causing it to return?

Reason behind implicit returns usage in Swift

It's only to look cleaner. There is no hidden performance gain, and even if there was, it would be so small nothing could track it. The only reason for it is to be cleaner and neater, as Swift is that. The rationale is that it will be easier to read.

Two returns in Closures

Like this:

var i = start
return /*the following is a closure that returns an Int:*/{
i += step
return i
}/*< end of closure*/

What's being returned is a closure. { return i } is actually a function that returns the value of i.

You could also write it this way:

func makeIterator(start: Int, step: Int) -> () -> Int {

var i = start
func iterate() -> Int {
i += step
return i
}
return iterate
}

A closure is functionality + state. The "state" here is the local variable i.

The closure "closes over" i, meaning that each time the closure is executed it will be looking at the same i which it can modify. This is true even after the closure is returned from the function. When you call it multiple times, it updates its internal state and returns the new value.

Return statement will never be executed warning

Good catch!

-Wunreachable-code does not report a warning and there is no other warning flag which would do.

Not even the Static Analyzer catches this mistake!

(Tested with XCode 6.1 GM 2)

Go to another view when NSURLSession finishes its job

The problem is that the completion handler code of the dataTaskWithURL method runs in a background secondary thread, not in the main thread (where view controller transition can happen).
Wrap the call to pushViewController in a main thread queue closure:

...
dispatch_async(dispatch_get_main_queue(), {

let navigationVC = self.navigationController
navigationVC?.pushViewController(weatherView, animated: false)
})
...

I have written the code in two lines to avoid a swift compiler bug (single statement and closure return value: see this). You can also write it as:

...
dispatch_async(dispatch_get_main_queue(), {
(self.navigationController)?.pushViewController(weatherView, animated: false)
return
})
...


Related Topics



Leave a reply



Submit