Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself.

Instance Methods

Instance methods are functions that belong to instances of a particular class, structure, or enumeration. They support the functionality of those instances, either by providing ways to access and modify instance properties, or by providing functionality related to the instance’s purpose. Instance methods have exactly the same syntax as functions, as described in Functions.

You write an instance method within the opening and closing braces of the type it belongs to. An instance method has implicit access to all other instance methods and properties of that type. An instance method can be called only on a specific instance of the type it belongs to. It cannot be called in isolation without an existing instance.

Here’s an example that defines a simple Counter class, which can be used to count the number of times an action occurs:

class Counter {
   var count = 0
   func increment() {
       count += 1

//  “amount” is parameter name
   func increment(by amount: Int) {
       count += amount
   func reset() {
       count = 0

The Counter class defines three instance methods:

let counter = Counter()
// the initial counter value is 0
// the counter’s value is now 1

// by is argument label
counter.increment(by: 5)
// the counter’s value is now 6
// the counter’s value is now 0

The self Property

Every instance of a type has an implicit property called self, which is exactly equivalent to the instance itself. You use the self property to refer to the current instance within its own instance methods.

The increment() method in the example above could have been written like this:

func increment() {
   self.count += 1

In practice, you don’t need to write self in your code very often. If you don’t explicitly write self, Swift assumes that you are referring to a property or method of the current instance whenever you use a known property or method name within a method. This assumption is demonstrated by the use of count (rather than self.count) inside the three instance methods for Counter.

The main exception to this rule occurs when a parameter name for an instance method has the same name as a property of that instance. In this situation, the parameter name takes precedence, and it becomes necessary to refer to the property in a more qualified way. You use the self property to distinguish between the parameter name and the property name.

Here, self disambiguates between a method parameter called x and an instance property that is also called x:

struct Point {
   var x = 0.0, y = 0.0
   func isToTheRightOf(x: Double) -> Bool {
       return self.x > x
let somePoint = Point(x: 4.0, y: 5.0)
if somePoint.isToTheRightOf(x: 1.0) {
   print(“This point is to the right of the line where x == 1.0”)
// Prints “This point is to the right of the line where x == 1.0”


(참고 tumblr #swift  #var  #let  #reference  #value  #type  #mutable  #immutable  #immutablity  #variable )

Modifying Value Types from Within Instance Methods

Structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods.

However, if you need to modify the properties of your structure or enumeration within a particular method, you can opt in to mutating behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends. The method can also assign a completely new instance to its implicit self property, and this new instance will replace the existing one when the method ends.

You can opt in to this behavior by placing the mutating keyword before the func keyword for that method:

struct Point {
   var x = 0.0, y = 0.0
   mutating func moveBy(x deltaX: Double, y deltaY: Double) {
       x += deltaX
       y += deltaY
var somePoint = Point(x: 1.0, y: 1.0)
somePoint.moveBy(x: 2.0, y: 3.0)
print(“The point is now at ((somePoint.x), (somePoint.y))”)
// Prints “The point is now at (3.0, 4.0)”

Note that you cannot call a mutating method on a constant of structure type, because its properties cannot be changed, even if they are variable properties, as described in Stored Properties of Constant Structure Instances:

let fixedPoint = Point(x: 3.0, y: 3.0)
fixedPoint.moveBy(x: 2.0, y: 3.0)
// this will report an error

Assigning to self Within a Mutating Method

Mutating methods can assign an entirely new instance to the implicit self property. The Point example shown above could have been written in the following way instead:

struct Point {
   var x = 0.0, y = 0.0
   mutating func moveBy(x deltaX: Double, y deltaY: Double) {
       self = Point(x: x + deltaX, y: y + deltaY)

Mutating methods for enumerations can set the implicit self parameter to be a different case from the same enumeration:

enum TriStateSwitch {
   case off, low, high
   mutating func next() {
       switch self {
       case .off:
           self = .low
       case .low:
           self = .high
       case .high:
           self = .off
var ovenLight = TriStateSwitch.low
// ovenLight is now equal to .high
// ovenLight is now equal to .off

Type Methods

You can also define methods that are called on the type itself. These kinds of methods are called type methods. You indicate type methods by writing the static keyword before the method’s func keyword. Classes may also use the class keyword to allow subclasses to override the superclass’s implementation of that method. (static 을 사용하여 type method를 만드는데 단 class의 경우 subclass에서 override를 해야 하는 경우 static대신 class를 사용한다는 설명)


In Swift, you can define type-level methods for all classes, structures, and enumerations. Each type method is explicitly scoped to the type it supports.

Type methods are called with dot syntax, like instance methods. However, you call type methods on the type, not on an instance of that type. Here’s how you call a type method on a class called SomeClass(클래스이름에 dot notation을 사용한다는 뜻):

class SomeClass {
   class func someTypeMethod() {
       // type method implementation goes here

Within the body of a type method, the implicit self property refers to the type itself, rather than an instance of that type. This means that you can use self to disambiguate between type properties and type method parameters, just as you do for instance properties and instance method parameters.

More generally, any unqualified method and property names that you use within the body of a type method will refer to other type-level methods and properties. A type method can call another type method with the other method’s name, without needing to prefix it with the type name. Similarly, type methods on structures and enumerations can access type properties by using the type property’s name without a type name prefix.

(일반적으로는 self를 앞에 붙이지 않고 같은 type내의 method, property에 접근 가능하다는 이야기)

성취된 level을 type내에서 공유하면서도 각개인의 level을 유지관리하는 코드 예시

struct LevelTracker {
   static var highestUnlockedLevel = 1
   var currentLevel = 1
   static func unlock(_ level: Int) {
       if level > highestUnlockedLevel { highestUnlockedLevel = level }
   static func isUnlocked(_ level: Int) -> Bool {
       return level <= highestUnlockedLevel
   mutating func advance(to level: Int) -> Bool {
       if LevelTracker.isUnlocked(level) {
           currentLevel = level
           return true
       } else {
           return false

class Player {
   var tracker = LevelTracker()
   let playerName: String
   func complete(level: Int) {
       LevelTracker.unlock(level + 1)
       tracker.advance(to: level + 1)
   init(name: String) {
       playerName = name

var player = Player(name: “Argyrios”)
player.complete(level: 1)
print(“highest unlocked level is now (LevelTracker.highestUnlockedLevel)”)
// Prints “highest unlocked level is now 2”

player = Player(name: “Beto”)
if player.tracker.advance(to: 6) {
   print(“player is now on level 6”)
} else {
   print(“level 6 has not yet been unlocked”)
// Prints “level 6 has not yet been unlocked”


Properties associate values with a particular class, structure, or enumeration. Stored properties store constant and variable values as part of an instance, whereas computed properties calculate (rather than store) a value. Computed properties are provided by classes, structures, and enumerations. Stored properties are provided only by classes and structures.

Stored and computed properties are usually associated with instances of a particular type. However, properties can also be associated with the type itself. Such properties are known as type properties.

In addition, you can define property observers to monitor changes in a property’s value, which you can respond to with custom actions. Property observers can be added to stored properties you define yourself, and also to properties that a subclass inherits from its superclass.

Stored Properties

Stored properties can be either variable stored properties (introduced by the varkeyword) or constant stored properties (introduced by the let keyword).

You can provide a default value for a stored property as part of its definition, as described in Default Property Values. You can also set and modify the initial value for a stored property during initialization. This is true even for constant stored properties, as described in Assigning Constant Properties During Initialization.

struct FixedLengthRange {
   var firstValue: Int
   let length: Int
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
// the range represents integer values 0, 1, and 2
rangeOfThreeItems.firstValue = 6
// the range now represents integer values 6, 7, and 8

let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
// this range represents integer values 0, 1, 2, and 3
rangeOfFourItems.firstValue = 6
// this will report an error, even though firstValue is a variable property

This behavior is due to structures being value types. When an instance of a value type is marked as a constant, so are all of its properties.

The same is not true for classes, which are reference types. If you assign an instance of a reference type to a constant, you can still change that instance’s variable properties.

Stored Properties of Constant Structure Instances

If you create an instance of a structure and assign that instance to a constant, you cannot modify the instance’s properties, even if they were declared as variable properties:

let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
// this range represents integer values 0, 1, 2, and 3
rangeOfFourItems.firstValue = 6
// this will report an error, even though firstValue is a variable property

This behavior is due to structures being value types. When an instance of a value type is marked as a constant, so are all of its properties.

The same is not true for classes, which are reference types. If you assign an instance of a reference type to a constant, you can still change that instance’s variable properties.

Lazy Stored Properties

A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.


You must always declare a lazy property as a variable (with the var keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.

class DataImporter {
    DataImporter is a class to import data from an external file.
    The class is assumed to take a nontrivial amount of time to initialize.
   var filename = “data.txt”
   // the DataImporter class would provide data importing functionality here
class DataManager {
   lazy var importer = DataImporter()
   var data = [String]()
   // the DataManager class would provide data management functionality here
let manager = DataManager()“Some data”)“Some more data”)
// the DataImporter instance for the importer property has not yet been created

Because it is marked with the lazy modifier, the DataImporter instance for the importer property is only created when the importer property is first accessed, such as when its filename property is queried:

// the DataImporter instance for the importer property has now been created
// Prints “data.txt”


If a property marked with the lazy modifier is accessed by multiple threads simultaneously and the property has not yet been initialized, there is no guarantee that the property will be initialized only once.

Computed Properties

getter and an optional setter to retrieve and set other properties and values indirectly.

struct Point {
   var x = 0.0, y = 0.0
struct Size {
   var width = 0.0, height = 0.0
struct Rect {
   var origin = Point()
   var size = Size()
   var center: Point {
       get {
           let centerX = origin.x + (size.width / 2)
           let centerY = origin.y + (size.height / 2)
           return Point(x: centerX, y: centerY)
       set(newCenter) {
           origin.x = newCenter.x – (size.width / 2)
           origin.y = newCenter.y – (size.height / 2)
var square = Rect(origin: Point(x: 0.0, y: 0.0),
                 size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = = Point(x: 15.0, y: 15.0)
print(“square.origin is now at ((square.origin.x), (square.origin.y))”)
// Prints “square.origin is now at (10.0, 10.0)”

Shorthand Setter Declaration

If a computed property’s setter does not define a name for the new value to be set, a default name of newValueis used. 

struct AlternativeRect {
   var origin = Point()
   var size = Size()
   var center: Point {
       get {
           let centerX = origin.x + (size.width / 2)
           let centerY = origin.y + (size.height / 2)
           return Point(x: centerX, y: centerY)

       // set(사용될변수이름) 

      //  이런 형태이나 아래와 같이 축약형으로사용 가능 newValue는 swift가          //  제공하는 기본 변수이름
       set {
           origin.x = newValue.x – (size.width / 2)
           origin.y = newValue.y – (size.height / 2)

Read-Only Computed Properties

A computed property with a getter but no setter is known as a read-only computed property. A read-only computed property always returns a value, and can be accessed through dot syntax, but cannot be set to a different value.


You must declare computed properties—including read-only computed properties—as variable properties with the var keyword, because their value is not fixed. The let keyword is only used for constant properties, to indicate that their values cannot be changed once they are set as part of instance initialization.

struct Cuboid {
   var width = 0.0, height = 0.0, depth = 0.0
   var volume: Double {
       return width * height * depth
let fourByFiveByTwo = Cuboid(width: 4.0, height: 5.0, depth: 2.0)
print(“the volume of fourByFiveByTwo is (fourByFiveByTwo.volume)”)
// Prints “the volume of fourByFiveByTwo is 40.0”

Property Observers

Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value.

You can add property observers to any stored properties you define, except for lazy stored properties. You can also add property observers to any inherited property (whether stored or computed) by overriding the property within a subclass. You don’t need to define property observers for nonoverridden computed properties, because you can observe and respond to changes to their value in the computed property’s setter. Property overriding is described in Overriding.

You have the option to define either or both of these observers on a property:

  • willSet is called just before the value is stored.
  • didSet is called immediately after the new value is stored.

If you implement a willSet observer, it’s passed the new property value as a constant parameter. You can specify a name for this parameter as part of your willSet implementation. If you don’t write the parameter name and parentheses within your implementation, the parameter is made available with a default parameter name of newValue.

Similarly, if you implement a didSet observer, it’s passed a constant parameter containing the old property value. You can name the parameter or use the default parameter name of oldValue. If you assign a value to a property within its own didSet observer, the new value that you assign replaces the one that was just set.


The willSet and didSet observers of superclass properties are called when a property is set in a subclass initializer, after the superclass initializer has been called. They are not called while a class is setting its own properties, before the superclass initializer has been called.

class StepCounter {
   var totalSteps: Int = 0 {
       willSet(newTotalSteps) {
           print(“About to set totalSteps to (newTotalSteps)”)
       didSet {
           if totalSteps > oldValue  {
               print(“Added (totalSteps – oldValue) steps”)
let stepCounter = StepCounter()
stepCounter.totalSteps = 200
// About to set totalSteps to 200
// Added 200 steps
stepCounter.totalSteps = 360
// About to set totalSteps to 360
// Added 160 steps
stepCounter.totalSteps = 896
// About to set totalSteps to 896
// Added 536 steps


If you pass a property that has observers to a function as an in-out parameter, the willSet and didSet observers are always called. This is because of the copy-in copy-out memory model for in-out parameters: The value is always written back to the property at the end of the function. For a detailed discussion of the behavior of in-out parameters, see In-Out Parameters.

Global and Local Variables

The capabilities described above for computing and observing properties are also available to global variables and local variables. Global variables are variables that are defined outside of any function, method, closure, or type context. Local variables are variables that are defined within a function, method, or closure context.

The global and local variables you have encountered in previous chapters have all been stored variables. Stored variables, like stored properties, provide storage for a value of a certain type and allow that value to be set and retrieved.

However, you can also define computed variables and define observers for stored variables, in either a global or local scope. Computed variables calculate their value, rather than storing it, and they are written in the same way as computed properties.


Global constants and variables are always computed lazily, in a similar manner to Lazy Stored Properties. Unlike lazy stored properties, global constants and variables do not need to be marked with the lazy modifier.

Local constants and variables are never computed lazily.

Type Properties

Instance properties are properties that belong to an instance of a particular type. 

You can also define properties that belong to the type itself, not to any one instance of that type. There will only ever be one copy of these properties.

Type properties are useful for defining values that are universal to all instances of a particular type, such as a constant property that all instances can use (like a static constant in C), or a variable property that stores a value that is global to all instances of that type (like a static variable in C).

Stored type properties can be variables or constants. Computed type properties are always declared as variable properties, in the same way as computed instance properties.


Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time.

Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier.

Type Property Syntax

type properties are written as part of the type’s definition, within the type’s outer curly braces, and each type property is explicitly scoped to the type it supports.

You define type properties with the static keyword. For computed type properties for class types, you can use the class keyword instead to allow subclasses to override the superclass’s implementation. The example below shows the syntax for stored and computed type properties:

struct SomeStructure {
   static var storedTypeProperty = “Some value.”
   static var computedTypeProperty: Int {
       return 1
enum SomeEnumeration {
   static var storedTypeProperty = “Some value.”
   static var computedTypeProperty: Int {
       return 6
class SomeClass {
   static var storedTypeProperty = “Some value.”
   static var computedTypeProperty: Int {
       return 27
   class var overrideableComputedTypeProperty: Int {
       return 107


The computed type property examples above are for read-only computed type properties, but you can also define read-write computed type properties with the same syntax as for computed instance properties.

Querying and Setting Type Properties

(type property는 클래스에 존재하는 상수,변수이며 이는 클래스이름을 통해 공유된다.)

Type properties are queried and set with dot syntax, just like instance properties. However, type properties are queried and set on the type, not on an instance of that type. For example:

// Prints “Some value.”
SomeStructure.storedTypeProperty = “Another value.”
// Prints “Another value.”
// Prints “6”
// Prints “27”

struct AudioChannel {
   static let thresholdLevel = 10
   static var maxInputLevelForAllChannels = 0
   var currentLevel: Int = 0 {
       didSet {
           if currentLevel > AudioChannel.thresholdLevel {
               // cap the new audio level to the threshold level
               currentLevel = AudioChannel.thresholdLevel
           if currentLevel > AudioChannel.maxInputLevelForAllChannels {
               // store this as the new overall maximum input level
               AudioChannel.maxInputLevelForAllChannels = currentLevel

var leftChannel = AudioChannel()
var rightChannel = AudioChannel()

leftChannel.currentLevel = 7
// Prints “7”
// Prints “7”

rightChannel.currentLevel = 11
// Prints “10”
// Prints “10”

// 위에와는 달리 할당되 새로운 값이 type property에 존재하는 것을 알수 있다.

Classes and Structures

Classes and structures are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your classes and structures by using exactly the same syntax as for constants, variables, and functions.

Unlike other programming languages, Swift does not require you to create separate interface and implementation files for custom classes and structures. In Swift, you define a class or a structure in a single file, and the external interface to that class or structure is automatically made available for other code to use.


An instance of a class is traditionally known as an object. However, Swift classes and structures are much closer in functionality than in other languages, and much of this chapter describes functionality that can apply to instances of either a class or a structure type.

Comparing Classes and Structures

Classes and structures in Swift have many things in common. Both can:

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Be extended to expand their functionality beyond a default implementation
  • Conform to protocols to provide standard functionality of a certain kind

For more information, see Properties, Methods, Subscripts, Initialization, Extensions, and Protocols.

Classes have additional capabilities that structures do not:

  • Inheritance enables one class to inherit the characteristics of another.
  • Type casting enables you to check and interpret the type of a class instance at runtime.
  • Deinitializers enable an instance of a class to free up any resources it has assigned.
  • Reference counting allows more than one reference to a class instance.

For more information, see Inheritance, Type Casting, Deinitialization, and Automatic Reference Counting.


Structures are always copied when they are passed around in your code, and do not use reference counting.

Definition Syntax

class keyword and structures with the struct keyword. 

class SomeClass {
   // class definition goes here
struct SomeStructure {
   // structure definition goes here


Whenever you define a new class or structure, you effectively define a brand new Swift type. Give types UpperCamelCase names (such as SomeClass and SomeStructure here). Conversely, always give properties and methods lowerCamelCase names (such as frameRate and incrementCount) to differentiate them from type names.

struct Resolution {
   var width = 0
   var height = 0
class VideoMode {
   var resolution = Resolution()
   var interlaced = false
   var frameRate = 0.0
   var name: String?

Class and Structure Instances:

let someResolution = Resolution()
let someVideoMode = VideoMode()

Structures and classes both use initializer syntax for new instances. 

Accessing Properties

You can access the properties of an instance using dot syntax.

print(“The width of someResolution is (someResolution.width)”)
// Prints “The width of someResolution is 0”

You can drill down into sub-properties, such as the width property in the resolution property of a VideoMode:

print(“The width of someVideoMode is (someVideoMode.resolution.width)”)
// Prints “The width of someVideoMode is 0”

someVideoMode.resolution.width = 1280
print(“The width of someVideoMode is now (someVideoMode.resolution.width)”)
// Prints “The width of someVideoMode is now 1280”


Unlike Objective-C, Swift enables you to set sub-properties of a structure property directly. In the last example above, the width property of the resolution property of someVideoMode is set directly, without your needing to set the entire resolution property to a new value.

Memberwise Initializers for Structure Types

All structures have an automatically-generated memberwise initializer, which you can use to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the memberwise initializer by name:

let vga = Resolution(width: 640, height: 480)

Unlike structures, class instances do not receive a default memberwise initializer. Initializers are described in more detail in Initialization.

Structures and Enumerations Are Value Types

A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.

You’ve actually been using value types extensively throughout the previous chapters. In fact, all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.

(참고 closure is reference type)

All structures and enumerations are value types in Swift. This means that any structure and enumeration instances you create—and any value types they have as properties—are always copied when they are passed around in your code.

let hd = Resolution(width: 1920, height: 1080)
var cinema = hd

cinema.width = 2048

print(“cinema is now (cinema.width) pixels wide”)
// Prints “cinema is now 2048 pixels wide”

print(“hd is still (hd.width) pixels wide”)
// Prints “hd is still 1920 pixels wide”

enum CompassPoint {
   case north, south, east, west
var currentDirection = CompassPoint.west
let rememberedDirection = currentDirection
currentDirection = .east
if rememberedDirection == .west {
   print(“The remembered direction is still .west”)
// Prints “The remembered direction is still .west”

Classes Are Reference Types

let tenEighty = VideoMode()
tenEighty.resolution = hd
tenEighty.interlaced = true = “1080i”
tenEighty.frameRate = 25.0

let alsoTenEighty = tenEighty
alsoTenEighty.frameRate = 30.0

print(“The frameRate property of tenEighty is now (tenEighty.frameRate)”)
// Prints “The frameRate property of tenEighty is now 30.0”

Identity Operators

Because classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of a class behind the scenes. (The same is not true for structures and enumerations, because they are always copied when they are assigned to a constant or variable, or passed to a function.)

It can sometimes be useful to find out if two constants or variables refer to exactly the same instance of a class. To enable this, Swift provides two identity operators:

  • Identical to (===)
  • Not identical to (!==)

if tenEighty === alsoTenEighty {
   print(“tenEighty and alsoTenEighty refer to the same VideoMode instance.”)
// Prints “tenEighty and alsoTenEighty refer to the same VideoMode instance.”

  • “Identical to” means that two constants or variables of class type refer to exactly the same class instance.
  • “Equal to” means that two instances are considered “equal” or “equivalent” in value, for some appropriate meaning of “equal”, as defined by the type’s designer.

Assignment and Copy Behavior for Strings, Arrays, and Dictionaries

In Swift, many basic data types such as String, Array, and Dictionary are implemented as structures. This means that data such as strings, arrays, and dictionaries are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.

This behavior is different from Foundation: NSString, NSArray, and NSDictionary are implemented as classes, not structures. Strings, arrays, and dictionaries in Foundation are always assigned and passed around as a reference to an existing instance, rather than as a copy.


The description above refers to the “copying” of strings, arrays, and dictionaries. The behavior you see in your code will always be as if a copy took place. However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization.

참고자료 :

Enumeration Syntax

enum SomeEnumeration {
   // enumeration definition goes here

enum CompassPoint {
   case north
   case south
   case east
   case west

Multiple cases can appear on a single line, separated by commas:

enum Planet {
   case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune

Each enumeration definition defines a brand new type. Like other types in Swift, their names (such as CompassPoint and Planet) should start with a capital letter. Give enumeration types singular rather than plural names, so that they read as self-evident:

var directionToHead = CompassPoint.west

The type of directionToHead is inferred when it is initialized with one of the possible values of CompassPoint. Once directionToHead is declared as a CompassPoint, you can set it to a different CompassPoint value using a shorter dot syntax:

directionToHead = .east

Matching Enumeration Values with a Switch Statement

directionToHead = .south
switch directionToHead {
case .north:
   print(“Lots of planets have a north”)
case .south:
   print(“Watch out for penguins”)
case .east:
   print(“Where the sun rises”)
case .west:
   print(“Where the skies are blue”)
// Prints “Watch out for penguins”

As described in Control Flow, a switch statement must be exhaustive when considering an enumeration’s cases. If the case for .west is omitted, this code does not compile.

When it is not appropriate to provide a case for every enumeration case, you can provide a default case to cover any cases that are not addressed explicitly:

let somePlanet =
switch somePlanet {
case .earth:
   print(“Mostly harmless”)
   print(“Not a safe place for humans”)
// Prints “Mostly harmless”

Associated Values

it is sometimes useful to be able to store associated values of other types alongside these case values. 

You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed. 

For example, suppose an inventory tracking system needs to track products by two different types of barcode. Some products are labeled with 1D barcodes in UPC format, which uses the numbers 0 to 9. Each barcode has a “number system” digit, followed by five “manufacturer code” digits and five “product code” digits. These are followed by a “check” digit to verify that the code has been scanned correctly:

Other products are labeled with 2D barcodes in QR code format, which can use any ISO 8859-1 character and can encode a string up to 2,953 characters long:

It would be convenient for an inventory tracking system to be able to store UPC barcodes as a tuple of four integers, and QR code barcodes as a string of any length.

enum Barcode {
   case upc(Int, Int, Int, Int)
   case qrCode(String)

This can be read as:

“Define an enumeration type called Barcode, which can take either a value of upc with an associated value of type (Int, Int, Int, Int), or a value of qrCode with an associated value of type String.”

This definition does not provide any actual Int or String values—it just defines the type of associated values that Barcode constants and variables can store when they are equal to Barcode.upc or Barcode.qrCode.

New barcodes can then be created using either type:

var productBarcode = Barcode.upc(8, 85909, 51226, 3)

productBarcode = .qrCode(“ABCDEFGHIJKLMNOP”)

At this point, the original Barcode.upc and its integer values are replaced by the new Barcode.qrCode and its string value. Constants and variables of type Barcode can store either a .upc or a .qrCode (together with their associated values), but they can only store one of them at any given time.

The different barcode types can be checked using a switch statement, as before. This time, however, the associated values can be extracted as part of the switch statement. You extract each associated value as a constant (with the let prefix) or a variable (with the var prefix) for use within the switch case’s body:

switch productBarcode {
case .upc(let numberSystem, let manufacturer, let product, let check):
   print(“UPC: (numberSystem), (manufacturer), (product), (check).”)
case .qrCode(let productCode):
   print(“QR code: (productCode).”)
// Prints “QR code: ABCDEFGHIJKLMNOP.”

If all of the associated values for an enumeration case are extracted as constants, or if all are extracted as variables, you can place a single var or let annotation before the case name, for brevity:

switch productBarcode {
case let .upc(numberSystem, manufacturer, product, check):
   print(“UPC : (numberSystem), (manufacturer), (product), (check).”)
case let .qrCode(productCode):
   print(“QR code: (productCode).”)
// Prints “QR code: ABCDEFGHIJKLMNOP.”

Raw Values

As an alternative to associated values, enumeration cases can come prepopulated with default values (called raw values), which are all of the same type.

enum ASCIIControlCharacter: Character {
   case tab = “t”
   case lineFeed = “n”
   case carriageReturn = “r”

Raw values can be strings, characters, or any of the integer or floating-point number types. Each raw value must be unique within its enumeration declaration.


Raw values are not the same as associated values. Raw values are set to prepopulated values when you first define the enumeration in your code, like the three ASCII codes above. The raw value for a particular enumeration case is always the same. Associated values are set when you create a new constant or variable based on one of the enumeration’s cases, and can be different each time you do so.

Implicitly Assigned Raw Values

enum Planet: Int {
   case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune

let earthsOrder =
// earthsOrder is 3
let sunsetDirection = CompassPoint.west.rawValue
// sunsetDirection is “west”

Initializing from a Raw Value

If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called rawValue) and returns either an enumeration case or nil. You can use this initializer to try to create a new instance of the enumeration.

This example identifies Uranus from its raw value of 7:

let possiblePlanet = Planet(rawValue: 7)
// possiblePlanet is of type Planet? and equals Planet.uranus

Not all possible Int values will find a matching planet, however. Because of this, the raw value initializer always returns an optional enumeration case. In the example above, possiblePlanet is of type Planet?, or “optional Planet.”


The raw value initializer is a failable initializer, because not every raw value will return an enumeration case. For more information, see Failable Initializers.

If you try to find a planet with a position of 11, the optional Planet value returned by the raw value initializer will be nil:

let positionToFind = 11
if let somePlanet = Planet(rawValue: positionToFind) {
   switch somePlanet {
   case .earth:
       print(“Mostly harmless”)
       print(“Not a safe place for humans”)
} else {
   print(“There isn’t a planet at position (positionToFind)”)
// Prints “There isn’t a planet at position 11”

This example uses optional binding to try to access a planet with a raw value of 11. The statement if let somePlanet = Planet(rawValue: 11) creates an optional Planet, and sets somePlanet to the value of that optional Planet if it can be retrieved. In this case, it is not possible to retrieve a planet with a position of 11, and so the else branch is executed instead.

Recursive Enumerations

A recursive enumeration is an enumeration that has another instance of the enumeration as the associated value for one or more of the enumeration cases. You indicate that an enumeration case is recursive by writing indirect before it, which tells the compiler to insert the necessary layer of indirection.

For example, here is an enumeration that stores simple arithmetic expressions:

enum ArithmeticExpression {
   case number(Int)
   indirect case addition(ArithmeticExpression, ArithmeticExpression)
   indirect case multiplication(ArithmeticExpression, ArithmeticExpression)

You can also write indirect before the beginning of the enumeration to enable indirection for all of the enumeration’s cases that have an associated value:

indirect enum ArithmeticExpression {
   case number(Int)
   case addition(ArithmeticExpression, ArithmeticExpression)
   case multiplication(ArithmeticExpression, ArithmeticExpression)

This enumeration can store three kinds of arithmetic expressions: a plain number, the addition of two expressions, and the multiplication of two expressions. The addition and multiplication cases have associated values that are also arithmetic expressions—these associated values make it possible to nest expressions. For example, the expression (5 + 4) * 2 has a number on the right-hand side of the multiplication and another expression on the left-hand side of the multiplication. Because the data is nested, the enumeration used to store the data also needs to support nesting—this means the enumeration needs to be recursive. The code below shows the ArithmeticExpression recursive enumeration being created for (5 + 4) * 2:

let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))

A recursive function is a straightforward way to work with data that has a recursive structure. For example, here’s a function that evaluates an arithmetic expression:

func evaluate(_ expression: ArithmeticExpression) -> Int {
   switch expression {
   case let .number(value):
       return value
   case let .addition(left, right):
       return evaluate(left) + evaluate(right)
   case let .multiplication(left, right):
       return evaluate(left) * evaluate(right)
// Prints “18”

This function evaluates a plain number by simply returning the associated value. It evaluates an addition or multiplication by evaluating the expression on the left-hand side, evaluating the expression on the right-hand side, and then adding them or multiplying them.

Enums in swift – Abhimuralidharan – Medium

What in the World is an “Escaping Closure” in Swift? – Andrew Bancroft

Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as closing over those constants and variables. Swift handles all of the memory management of capturing for you.

Global and nested functions, as introduced in Functions, are actually special cases of closures. Closures take one of three forms:

  • Global functions are closures that have a name and do not capture any values.
  • Nested functions are closures that have a name and can capture values from their enclosing function.
  • Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.

The Sorted Method

기본 라이브러리에 sorted()라는 함수가 있는데 어떻게 분류하는지에 대한 부분을 함수형태의 argument로 받아들인다. closure의 사용예를 위해 sorted()를 사용했다.

일반 함수를 이용한 방법

func backward(_ s1: String, _ s2: String) -> Bool {
   return s1 > s2
var reversedNames = names.sorted(by: backward)
// reversedNames is equal to [“Ewa”, “Daniella”, “Chris”, “Barry”, “Alex”]

Closure Expression Syntax

{ (parameters) -> return type in

closure를 이용한 방법

reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
   return s1 > s2

Inferring Type From Context

Because the sorting closure is passed as an argument to a method, Swift can infer the types of its parameters and the type of the value it returns. The sorted(by:) method is being called on an array of strings, so its argument must be a function of type (String, String) -> Bool. This means that the (String, String) and Bool types do not need to be written as part of the closure expression’s definition. Because all of the types can be inferred, the return arrow (->) and the parentheses around the names of the parameters can also be omitted:

reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )

It is always possible to infer the parameter types and return type when passing a closure to a function or method as an inline closure expression. As a result, you never need to write an inline closure in its fullest form when the closure is used as a function or method argument.

Implicit Returns from Single-Expression Closures

Single-expression closures can implicitly return the result of their single expression by omitting the return keyword from their declaration, as in this version of the previous example:

reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )

Here, the function type of the sorted(by:) method’s argument makes it clear that a Bool value must be returned by the closure. Because the closure’s body contains a single expression (s1 > s2) that returns a Bool value, there is no ambiguity, and the return keyword can be omitted.

Shorthand Argument Names

Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.

If you use these shorthand argument names within your closure expression, you can omit the closure’s argument list from its definition, and the number and type of the shorthand argument names will be inferred from the expected function type. The in keyword can also be omitted, because the closure expression is made up entirely of its body:

reversedNames = names.sorted(by: { $0 > $1 } )

Here, $0 and $1 refer to the closure’s first and second String arguments.

Operator Methods

There’s actually an even shorter way to write the closure expression above. Swift’s String type defines its string-specific implementation of the greater-than operator (>) as a method that has two parameters of type String, and returns a value of type Bool. This exactly matches the method type needed by the sorted(by:)method. Therefore, you can simply pass in the greater-than operator, and Swift will infer that you want to use its string-specific implementation:

reversedNames = names.sorted(by: >)

For more about operator method, see Operator Methods.

Trailing Closures

If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the closure as part of the function call.

func someFunctionThatTakesAClosure(closure: () -> Void) {
   // function body goes here
// Here’s how you call this function without using a trailing closure(일반방법)
someFunctionThatTakesAClosure(closure: {
   // closure’s body goes here


// Here’s how you call this function with a trailing closure instead:
someFunctionThatTakesAClosure() {
   // trailing closure’s body goes here

The string-sorting closure from the Closure Expression Syntax section above can be written outside of the sorted(by:) method’s parentheses as a trailing closure:

reversedNames = names.sorted() { $0 > $1 }

If a closure expression is provided as the function or method’s only argument and you provide that expression as a trailing closure, you do not need to write a pair of parentheses () after the function or method’s name when you call the function:

reversedNames = names.sorted { $0 > $1 }


let digitNames = [
   0: “Zero”, 1: “One”, 2: “Two”,   3: “Three”, 4: “Four”,
   5: “Five”, 6: “Six”, 7: “Seven”, 8: “Eight”, 9: “Nine”
let numbers = [16, 58, 510]

let strings = { (number) -> String in
   var number = number
   var output = “”
   repeat {
       output = digitNames[number % 10]! + output
       number /= 10
   } while number > 0
   return output
// strings is inferred to be of type [String]
// its value is [“OneSix”, “FiveEight”, “FiveOneZero”]

Capturing Values

A closure can capture constants and variables from the surrounding context in which it is defined. The closure can then refer to and modify the values of those constants and variables from within its body, even if the original scope that defined the constants and variables no longer exists.

In Swift, the simplest form of a closure that can capture values is a nested function, written within the body of another function. A nested function can capture any of its outer function’s arguments and can also capture any constants and variables defined within the outer function.

// ()->int meant reture type is function type

func makeIncrementer(forIncrement amount: Int) -> () -> Int {
   var runningTotal = 0
   func incrementer() -> Int {
       runningTotal += amount
       return runningTotal

// return closure
   return incrementer

func incrementer() -> Int {
   runningTotal += amount
   return runningTotal

함수내에서 만들어진 nested 함수는 nesting 함수의 변수에 접근 가능하고 

이는 마치 python의 class variable 처럼 계속 남아 유지 하게 되며 nested 함수에서 수시로 접근 가능하다.

let incrementByTen = makeIncrementer(forIncrement: 10)

// returns a value of 10
// returns a value of 20
// returns a value of 30

// new nested function is made and will be used differently from above

let incrementBySeven = makeIncrementer(forIncrement: 7)
// returns a value of 7

// returns a value of 40

Closures Are Reference Types

In the example above, incrementBySeven and incrementByTen are constants, but the closures these constants refer to are still able to increment the runningTotal variables that they have captured. This is because functions and closures are reference types.

Whenever you assign a function or a closure to a constant or a variable, you are actually setting that constant or variable to be a reference to the function or closure. In the example above, it is the choice of closure that incrementByTen refers to that is constant, and not the contents of the closure itself.

This also means that if you assign a closure to two different constants or variables, both of those constants or variables will refer to the same closure:

let alsoIncrementByTen = incrementByTen
// returns a value of 50

Escaping Closures

참고자료 : ref)

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

One way that a closure can escape is by being stored in a variable that is defined outside the function. As an example, many functions that start an asynchronous operation take a closure argument as a completion handler. The function returns after it starts the operation, but the closure isn’t called until the operation is completed—the closure needs to escape, to be called later. For example:

var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {

The someFunctionWithEscapingClosure(_:) function takes a closure as its argument and adds it to an array that’s declared outside the function. If you didn’t mark the parameter of this function with @escaping, you would get a compile-time error.

Marking a closure with @escaping means you have to refer to self explicitly within the closure. For example, in the code below, the closure passed to someFunctionWithEscapingClosure(_:) is an escaping closure, which means it needs to refer to self explicitly. In contrast, the closure passed to someFunctionWithNonescapingClosure(_:) is a nonescaping closure, which means it can refer to self implicitly.

func someFunctionWithNonescapingClosure(closure: () -> Void) {
class SomeClass {
   var x = 10
   func doSomething() {
       someFunctionWithEscapingClosure { self.x = 100 }
       someFunctionWithNonescapingClosure { x = 200 }
let instance = SomeClass()
// Prints “200”
// Prints “100″


An autoclosure is a closure that is automatically created to wrap an expression that’s being passed as an argument to a function. It doesn’t take any arguments, and when it’s called, it returns the value of the expression that’s wrapped inside of it. This syntactic convenience lets you omit braces around a function’s parameter by writing a normal expression instead of an explicit closure.

It’s common to call functions that take autoclosures, but it’s not common to implement that kind of function. For example, the assert(condition:message:file:line:) function takes an autoclosure for its condition and messageparameters; its condition parameter is evaluated only in debug builds and its message parameter is evaluated only if condition is false.

An autoclosure lets you delay evaluation, because the code inside isn’t run until you call the closure. Delaying evaluation is useful for code that has side effects or is computationally expensive, because it lets you control when that code is evaluated. The code below shows how a closure delays evaluation.

var customersInLine = ["Chris”, “Alex”, “Ewa”, “Barry”, “Daniella”]
// Prints “5”
let customerProvider = { customersInLine.remove(at: 0) }
// Prints “5”
print(“Now serving (customerProvider())!”)
// Prints “Now serving Chris!”
// Prints “4”

Even though the first element of the customersInLine array is removed by the code inside the closure, the array element isn’t removed until the closure is actually called. If the closure is never called, the expression inside the closure is never evaluated, which means the array element is never removed. Note that the type of customerProvider is not String but () -> String—a function with no parameters that returns a string.

You get the same behavior of delayed evaluation when you pass a closure as an argument to a function.

// customersInLine is [“Alex”, “Ewa”, “Barry”, “Daniella”]
func serve(customer customerProvider: () -> String) {
   print(“Now serving (customerProvider())!”)
serve(customer: { customersInLine.remove(at: 0) } )
// Prints “Now serving Alex!”

The serve(customer:) function in the listing above takes an explicit closure that returns a customer’s name. The version of serve(customer:) below performs the same operation but, instead of taking an explicit closure, it takes an autoclosure by marking its parameter’s type with the @autoclosure attribute. Now you can call the function as if it took a String argument instead of a closure. The argument is automatically converted to a closure, because the customerProvider parameter’s type is marked with the @autoclosure attribute.

// customersInLine is [“Ewa”, “Barry”, “Daniella”]
func serve(customer customerProvider: @autoclosure () -> String) {
   print(“Now serving (customerProvider())!”)
serve(customer: customersInLine.remove(at: 0))
// Prints “Now serving Ewa!”


Overusing autoclosures can make your code hard to understand. The context and function name should make it clear that evaluation is being deferred.

If you want an autoclosure that is allowed to escape, use both the @autoclosure and @escaping attributes. The @escaping attribute is described above in Escaping Closures.

// customersInLine is [“Barry”, “Daniella”]
var customerProviders: [() -> String] = []
func collectCustomerProviders(_ customerProvider: @autoclosure @escaping () -> String) {
collectCustomerProviders(customersInLine.remove(at: 0))
collectCustomerProviders(customersInLine.remove(at: 0))
print(“Collected (customerProviders.count) closures.”)
// Prints “Collected 2 closures.”
for customerProvider in customerProviders {
   print(“Now serving (customerProvider())!”)
// Prints “Now serving Barry!”
// Prints “Now serving Daniella!”

In the code above, instead of calling the closure passed to it as its customerProvider argument, the collectCustomerProviders(_:) function appends the closure to the customerProviders array. The array is declared outside the scope of the function, which means the closures in the array can be executed after the function returns. As a result, the value of the customerProvider argument must be allowed to escape the function’s scope.