0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】zip関数をはじめて使った

Posted at

はじめに

zip関数は配列や辞書などをタプルにしてくれる機能らしいです。

2つの配列をタプルの配列に変換しています。

let alphabets = ["A", "B", "C"]
let index = [1, 2, 3]

let zipped = Array(zip(alphabets, index))

print(zipped)
// [(A, 1), (B, 2), (C, 3)]

では、配列の中の個数が違う場合はどのようになるでしょうか?

let alphabets = ["A", "B", "C"]
let index = [1, 2]

let zipped = Array(zip(alphabets, index))

print(zipped)
// [(A, 1), (B, 2)]

少ないほうに合わせてタプルが生成されました。

おわり

難しいと思っていましたが、意外と単純なものでした。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?