Bibi's DevLog πŸ€“πŸŽ

[Swift] .zip(_:_:) : 두 μ‹œν€€μŠ€ ν•©μΉ˜κΈ° λ³Έλ¬Έ

πŸ“±πŸŽ iOS/🍏 Apple Developer Documentation

[Swift] .zip(_:_:) : 두 μ‹œν€€μŠ€ ν•©μΉ˜κΈ°

λΉ„λΉ„ bibi 2022. 6. 21. 23:22

[Swift] .zip(_:_:) : 두 μ‹œν€€μŠ€ ν•©μΉ˜κΈ°

https://developer.apple.com/documentation/swift/zip(_:_:)

두 μ‹œν€€μŠ€λ‘œλΆ€ν„° λ§Œλ“  μ‹œν€€μŠ€ μŒμ„ λ§Œλ“œλŠ” λ©”μ„œλ“œ

func zip<Sequence1, Sequence2>(
    _ sequence1: Sequence1,
    _ sequence2: Sequence2
) -> Zip2Sequence<Sequence1, Sequence2> where Sequence1 : Sequence, Sequence2 : Sequence
  • 리턴값

    • νŠœν”Œ 쌍의 μ‹œν€€μŠ€λ₯Ό λ¦¬ν„΄ν•œλ‹€.
    • 각 μŒμ€ μ‹œν€€μŠ€1κ³Ό μ‹œν€€μŠ€2의 μš”μ†Œλ“€μ— μƒμ‘ν•œλ‹€.
  • νŒŒλΌλ―Έν„°

    • μ‹œν€€μŠ€1, 2 : 각각 zipν•  첫 번째, 두 번쨰 μ‹œν€€μŠ€ λ˜λŠ” μ»¬λ ‰μ…˜
  • μš”μ†Œλ₯Ό 직접 μ‚¬μš©ν•˜λ €λ©΄ element.0, element.1 처럼 μ‚¬μš©ν•  수 μžˆμ§€λ§Œ, νŠœν”Œλ‘œ 각 μ‹œν€€μŠ€λ₯Ό μ΄λ¦„λΆ™μ—¬μ„œ μ‚¬μš©ν•˜λŠ” 게 더 νŽΈν•˜λ‹€

  • λ§Œμ•½ 두 μ‹œν€€μŠ€μ˜ 길이가 λ‹€λ₯΄λ©΄, 짧은 μͺ½μ˜ μ‹œν€€μŠ€ 길이에 λ§žμΆ”μ–΄ zip이 μƒμ„±λœλ‹€.

  • 예제 μ½”λ“œ

    • let words = ["one", "two", "three", "four"]
      let numbers = 1...4
      
      for (word, number) in zip(words, numbers) {
          print("\(word): \(number)")
      }
      // Prints "one: 1"
      // Prints "two: 2
      // Prints "three: 3"
      // Prints "four: 4"