Skip to content

Instantly share code, notes, and snippets.

@pine
Last active March 1, 2016 10:55
Show Gist options
  • Save pine/f85f9f3e632d5011015d to your computer and use it in GitHub Desktop.
Save pine/f85f9f3e632d5011015d to your computer and use it in GitHub Desktop.
shibuya.swift #3

既存の Objective-C のコードをいかに Swift へ移管するか

Swift が発表されて 2 年経つ。 そろそろ、重い腰を上げて、移行する時期ではないだろうか。

延命パターンを提案

ベースを Swift にして、既存の Objective-C のコードを Swift から使いやすくする。 Swift からみたみたインターフェイスを改善する。

Nullability

  • nullable
  • nonnull
  • null resetable
  • NS_ASSUME_NONNULL
    • 範囲指定で nonnull が指定できる

Lightweight Generics

Swift からはジェネリクスとして見える。

Generated Interface

Swift から見たインターフェイスを Xcode 上から表示できる

Swift への移行手順

  1. とりあえず Optional にする
  • Swift から利用する最低限のインターフェイスを確保できる
  • nonnull を指定するには、きちんとコードを読まなくてはいけなくて面倒
  1. nonnull を指定する
  • 本当に nonnull になるか確認が必要
  • Swift から扱いやすくなる
  1. Generics にする
  • Swift から見た時に AnyObject を見るとため息が出る
  • 型がある配列として扱えると幸せ

まとめ

既存の Objective-C のコードを Swift から利用しやすくし、段階的に Swift へ移行しよう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment