본문 바로가기
Swift

[Swift] @discardableResult

by thoonk: 2021. 7. 18.

@discardableResult에 관해 기록합니다.

 

Return value가 있어 호출했을 때 "_" 나 변수에 할당해주지 않으면

'Result of call to ~~~ is unused' warning이 뜨게 된다. 

 

하지만 return value가 있는데 이 값을 사용하지 않을 때가 있다. 

그 때 @discardableResult를 함수 앞에 선언해서 warning이 뜨는 것을 예방할 수 있다.

 

@discardableResult
func discardableInfo() -> String {
    let infoString = "Info"
    return infoString
}

discardableInfo() // Result unused warning이 뜨지 않는 것을 확인할 수 있음.

 

참고:

https://www.hackingwithswift.com/example-code/language/how-to-ignore-return-values-using-discardableresult

'Swift' 카테고리의 다른 글

[Swift] Equatable, Hashable  (0) 2023.01.03
[Swift] split vs components  (0) 2021.06.05
[Swift] Optional  (0) 2021.05.22
[Swift] ARC (1)  (0) 2021.05.12
[Swift] popLast() vs removeLast()  (0) 2021.05.11

댓글