본문 바로가기

IOS 개발

[iOS] func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T) rethrows -> T

??

func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T) rethrows -> T

 

 

요약
nil-coalescing 작업 (무-응집 작업)을 수행하여 Optional 인스턴스의 래핑된 값 또는 기본값을 반환합니다.

Summary

Performs a nil-coalescing operation, returning the wrapped value of an Optional instance or a default value.

 

Declaration

func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T) rethrows -> T

 

 

토론
nil-coalescing(무-응집) 연산은 값이 있으면 왼쪽을 풀거나 오른쪽을 기본값으로 반환합니다. 이 작업의 결과는 비 선택적 유형의 왼쪽 포장 유형을 갖게됩니다.

이 연산자는 단락 평가를 사용합니다. 선택 사항이 먼저 확인되고 선택 사항이 nil 인 경우에만 기본값이 평가됩니다. 예를 들면 다음과 같습니다.

Discussion

A nil-coalescing operation unwraps the left-hand side if it has a value, or it returns the right-hand side as a default. The result of this operation will have the non-optional type of the left-hand side’s Wrapped type.

This operator uses short-circuit evaluation: optional is checked first, and defaultValue is evaluated only if optional is nil. For example:

 

이 예에서, Int ( "100")가 0이 아닌 결과를 리턴하는 데 성공했기 때문에 goodNumber에 값 100이 지정됩니다. notSoGoodNumber가 초기화되면 Int ( "invalid-input")가 실패하고 nil을 리턴하므로 getDefault () 메소드가 호출되어 기본값을 제공합니다.

In this example, goodNumber is assigned a value of 100 because Int("100") succeeded in returning a non-nil result. When notSoGoodNumber is initialized, Int("invalid-input") fails and returns nil, and so the getDefault() method is called to supply a default value.

 

매개 변수

optional

An optional value.

기본값

기본값으로 사용할 값입니다. 기본값은 Wrappedtype과 선택적 유형입니다

Parameters

optional

An optional value.

defaultValue

A value to use as a default. defaultValue is the same type as the Wrapped type of optional.

 

 

 

 

[참조 : Open in Developer Documentation]

'IOS 개발' 카테고리의 다른 글

[iOS] class LAContext : NSObject  (0) 2020.04.03
[iOS] class UIApplication : UIResponder  (0) 2020.04.03
[iOS] struct UIEdgeInsets  (0) 2020.03.31
[iOS] class UIImage : NSObject  (0) 2020.03.31
[iOS] class UIControl : UIView  (0) 2020.03.31