본문 바로가기

iOS/STUDY

[Swift] Int - quotientAndRemainder(dividingBy:)

문서에서 Int 의 Instance Method를 구경하던 도중 발견한 함수!

최근에 몫과 나머지를 사용한 문제를 종종 풀었는데 그때마다 직접 나누고 나머지를 따로 구했는데

한번에 구할 수 있는 함수를 알아두기 위해서 기록기록 

 

func quotientAndRemainder(dividingBy rhs: Int) -> (quotient: Int, remainder: Int)
let x = 1_000_000
let (q, r) = x.quotientAndRemainder(dividingBy: 933)
// q == 1071
// r == 757

 

(몫, 나머지) 형태로 반환해준다.

x 선언할 때 _ 을 쓰는건 처음본다.. 신기하니 기억해두자