swift とObjective-Cの比較

関数の呼び出しの違い。
(selectorとしてkeyboardWillShow(ファンクション)を設定している。)
Objective-C

[
 [NSNotificationCenter defaultCenter] 
  addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil
];

swift

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector:"keyboardWillShow:", 
name: UIKeyboardWillShowNotification, object:nil)

引数に構文を使う場合
Objective-C → ブロック
(ブロックリテラルの始まりを表すには、「^」キャロット演算子を使用。)

[UIView animateWithDuration:duration 
      animations:^{
                [self.view layoutIfNeeded];
      }
];

swift → クロージャ

UIView.animateWithDuration(duration,
    animations: {() -> Void  in
            self.view.layoutIfNeeded()
    }
)

コメント