4
3

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 5 years have passed since last update.

Swiftのinについて

Posted at

Swiftのinは次のような繰り返し処理で利用します。

var numbers = [1, 2, 3]
for number in numbers {
	println(number * number)
}

inはまた、次のようなクロージャの構文でも利用されます。

var numbers = [1, 2, 3]
var squares = numbers.map({(number) -> Int in
    var square = number * number
    return square
})

これらは用途が全く異なるため、少し戸惑うかもしれません。

また、クロージャのinの後ろには処理を書きますが、上記のように複数行の処理も記述可能です。
複数行の処理は全体を{}で括りたくなりますが、実際に試してみると構文エラーになってしまいます。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?