공지사항

  • 안녕하세요, 오뜨입니다 !
ohtt
오뜨네
ohtt
  • 분류 전체보기 (36)
    • iOS (17)
      • iOS (8)
      • swift (4)
      • Reactive (1)
      • swiftUI (4)
    • Algorithm (0)
      • 자료구조 (0)
      • 알고리즘 (0)
      • 문제 (0)
    • 기타 (3)
      • Git (1)
      • Design (1)
      • ETC. (1)
    • 끄적끄적 (9)
      • 회고 (2)
      • 일상 (6)
      • 폰으로끄적임 (0)
    • Books (7)
전체 방문자
오늘
어제

태그

  • ios
  • 회고
  • autorenewal
  • 힙주비
  • SWIFT
  • 책
  • isHidden
  • iOS16
  • discardableResult
  • swiftUI

블로그 메뉴

  • 홈
  • 태그
  • 방명록

최근 댓글

hELLO · Designed By 정상우.
ohtt

오뜨네

iOS/swift

[swift] Collection - indices

2022. 4. 12. 00:01

별 내용 아니지만 몰랐던 방식이라 적어두기 :)

 

for index in 0..<numbers.count {
  print(numbers[index])
}

for (index, element) in numbers.enumerated() {
  print(element)
}

numbers.forEach { num in
  print(num)
}

어떤 배열을 순회하는 방식에는 여러가지가 있다 

위에 적혀있는 방식 말고도 또 다른 방식들도 있는데 주로 사용하는 방식은 위와 같다.

 

배열을 순회하면서 그 index에 맞는 처리를 해줄 때는 가장 위의 것을 사용하곤 하는데 

indices라는 property를 사용하면 조금 더 깔끔하게 작성할 수 있다 !  

+ forEach문과도 사용할 수 있음

for index in numbers.indices {
  print(numbers[index])
}

numbers.indices.forEach {
  print(numbers[$0])
}

'iOS > swift' 카테고리의 다른 글

[swift] @discardableResult 키워드  (0) 2022.04.16
[Swift] if let 과 guard let  (0) 2022.02.20
[Swift] class / struct - initializer - 조금 더 편하게 작성하는 법  (0) 2022.01.21
    'iOS/swift' 카테고리의 다른 글
    • [swift] @discardableResult 키워드
    • [Swift] if let 과 guard let
    • [Swift] class / struct - initializer - 조금 더 편하게 작성하는 법
    ohtt
    ohtt
    iOS Developer

    티스토리툴바