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

swift4.2の変更点

Posted at

4.2が出てだいぶ時間もたったのですが、現状まだ4.0を使ってるので備忘録として変更点をまとめておきます。

Bool型を .toggle()で反転できる

true or false の反転がしやすくなった

    var isMoved = false
    isMoved.toggle() // trueになる
    
    // こうも使える
    sampleView.isHidden.togle()

self = self

selfのアンラップ時に

  guard let `self` = self else { return } 

って書いてたのがそもそもコンパイラのバグで、 4.2からは以下のように書けるようになりました。

  guard let self = self else { return } 

ランダム周りの変更

かなり便利になった
arc4randomとのお別れ。UIntから変換する必要もなくなった。

// 0から99のどれか ex> 10
let randomNumber = Int.random(in: 0 ... 99) 

let array = ["hoge", "huga", "piyo"]

// 配列のうちどれか ex> "huga"
let randomElement = array.randomElement() 
// 順番の入れ替え ex> ["piyo", "hoge", "huga"]
let shuffledArray = array.shuffled() 


2
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
2
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?