Skip to main content

Closures can now be marked @noescape

About 2 minSwiftArticle(s)bloghackingwithswift.comswiftswift-1.2

Closures can now be marked @noescape 관련

HACKING WITH SWIFT

What's new in Swift?

Closures can now be marked @noescape | Changes in Swift 1.2

Closures can now be marked @noescape

Available from Swift 1.2

Closures are reference types, which means Swift must quietly add memory management calls when they are passed into functions. To avoid adding unwanted work, you can now mark closure parameters with the @noescape keyword, which tells Swift the closure will be used before the function returns – it doesn't need to retain or release the closure.

As an example, this function checks whether a password that we have stored matches a password the user just entered, but it does this using a closure so that you can give it any encryption code you like. This closure is used immediately inside the function, so @noescape may be used as a performance optimization:

func checkPassword(encryption: @noescape (String) -> ()) -> Bool {
    if closure(enteredPassword) == storedPassword {
        return true
    } else {
        return false
    }
}

Note

This has changed in later versions of Swift – all closures are considered to be non-escaping by default.

Other changes in Swift 1.2…
The zip() function joins two sequences | Changes in Swift 1.2

The zip() function joins two sequences
The flatMap() method transforms optionals and arrays | Changes in Swift 1.2

The flatMap() method transforms optionals and arrays
Classes can now have static methods and properties | Changes in Swift 1.2

Classes can now have static methods and properties
Constants no longer require immediate initialization | Changes in Swift 1.2

Constants no longer require immediate initialization
A new Set data structure | Changes in Swift 1.2

A new Set data structure
Implicit bridging has been reduced | Changes in Swift 1.2

Implicit bridging has been reduced
Multiple if let bindings | Changes in Swift 1.2

Multiple if let bindings
Typecasting now includes as! | Changes in Swift 1.2

Typecasting now includes as!

이찬희 (MarkiiimarK)
Never Stop Learning.