[swift-users] Capture values by Closures in different background threads.

Jordan Rose jordan_rose at apple.com
Wed Nov 30 11:10:06 CST 2016


When not using a capture list, Swift closures always refer to variables, not to their values. So here you’re not capturing copies, you’re capturing the actual variable ‘test’, and the mutating function will update it. (If you did capture a copy, it would be immutable, and the call to ‘newNum(new:)’ would be rejected.)

On top of that, ‘test’ is a top-lovel variable, so it doesn’t even need to be captured at all. The closures can just refer to it globally.

Hope that clears things up. There are some more examples of this <https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/doc/uid/TP40014097-CH32-ID544> in the Swift Programming Language book.

Jordan


> On Nov 30, 2016, at 02:38, Седых Александр via swift-users <swift-users at swift.org> wrote:
> 
> Assume we have this code:
>  
> Immediately two Timer's closures captured two copy of initial values of instance Test, with internal num values is 0.
>  
> //: Playground - noun: a place where people can play
>  
> import UIKit
>  
> var str = "Hello, playground"
>  
> struct Test {
>     var num = 0
>     mutatingfunc newNum(new: Int) {
>         num = new
>     }
> }
>  
> var test = Test()
>  
> Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in
>     print("tick")
>     test.newNum(new: 8)
> }
>  
> Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { (timer) in
>     print("tack, test.num = \(test.num)")
> }
>  
>  
> CFRunLoopRun()
> 
> 
> 
> We have next log:
> tick
> tack, test.num = 8
>  
> Why Timer's two closure return to us value 8 and not 0?
> 
> 
> 
> -- 
> Седых Александр
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161130/6e11c29e/attachment.html>


More information about the swift-users mailing list