Basics Closure

Run Settings
LanguageSwift
Language Version
Run Command
print("1. Capture a var of a Value Type:") var number = 10 let closure: () -> Void = { number += 5 } closure() print(number) // Output: 15 print("2. Capture a var of a Reference Type:") class NumberHolder { var number = 10 } var holder = NumberHolder() let closure2: () -> Void = { holder.number += 5 } closure2() print(holder.number) // Output: 15 print("[weak holder] ") var holder: NumberHolder? = NumberHolder() let closure3: () -> Void = { [weak holder] in holder?.number += 5 } closure3() print(holder?.number ?? 0) // Output: 15
import Foundation
Editor Settings
Theme
Key bindings
Full width
Lines