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

【備忘録】Swift 実践入門について 第4章

Last updated at Posted at 2020-11-30

Swift 実践入門のまとめ。
分からない部分の抜粋も記載し、解決できたら随時更新していきます。
なお、ここに記載している以外でも「わけわからん…」となっている部分も多々ありますが、
今は必要ない、と言い聞かせて飛ばしています。

第4章
コレクション let a = [1, 2, 3]など要素を複数持ちたいとき。
理解度:80%くらいか

4.2
Array型…[1, 2, 3]

4.3
Dictionary型…let dictionary = [“a” : 1, “b” : 2] キーワードの“Key”とValue

4.4
範囲型…let range = 1..< 4

4.5
String型のコレクション
Character型…単一文字を示す
String.Index型…startIndexやendIndexなど

4.6
シーケンス…値にアクセスしていく順番。普通は左から順番にアクセスしていくが変更も可能。

・filter…複数のエレメントを持つ変数aに対してlet b = a.filter({element in element % 2 == 0})とすれば、定数bは変数aの偶数番目のエレメントを持つようになる。

・map…全エレメントに変更を加える。let b = a.map({element in element * 2})とすれば、定数bは変数aを2倍したエレメントを持つようになる。

・flatMap…let b = a.flatMap({value in [value, value + 1]})とすれば、定数bはaのエレメントとそのエレメントに1を足した値をそれぞれ持つ。

・compactMap…mapに似ているが、変更できない値があった場合は無視してコンパクトにする。

・コレクションのプロトコル
let a = [1, 2, 3, 4, 5]
a[2] //3
a.isEmpty // false
a.count //5
a.first //1
a.last //5

「.プロパティ」にまだ慣れない。本を読んでいてもSwift側で決められたプロパティなのか、こちらが個人的に決めたものなのかが曖昧。ただ4章まで進めてきて、少しづつ理解が深まってきている気はする。

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?