[ 4/3 12:46 マッハで追記 ]
この記事は、Swift4 -> 5への変更ではなく、4 -> 4.2で発生した差だそうです。
Daiki Matsudateさん、ご指摘ありがとうございます。
これはSwift5での変更ではなく、Xcode10(Swift4.2)での変更なのと、マイグレーターがあるので、大した変更ではないです。
— Daiki Matsudate (@d_date) April 3, 2019
という訳で、4.0から5.0への変更に関するエラーはお薬アラームでは無かったので、4.2の人はより安心してSwift5へバージョンアップしても良さそうです。
具体的にSwift5への変更で発生しそうな問題はこんな感じだそうです↓
Swift 5のマイグレーションは、functionについてる冗長なmodifier、nested try?、enumの@ unknown default、OptionalからAnyへのキャスト、index(of:)をfirstIndex(of:)に直して終了かね。
— Daiki Matsudate (@d_date) April 2, 2019
[ 以下は元記事そのまま ]
XCode10.2から、Swift5への対応が開始されましたね。
きっと、近い内にSwift5以外選べなくなると思うので、傷が広がる前に、運用中のアプリの1つをSwift4から5に変えてみました。
変えてみたアプリはコチラ↓
しつこいお薬アラーム
お薬の時間をアラームで知らせるシンプルなアプリです。
DAUは3,500ぐらい、売上は毎月15,000円前後をフラフラしている規模のアプリになります。
アラームアプリでそんなに儲かるの!?の秘密については、この記事にまとめていますのでついでに見て、参考にしてみてください♪
http://toconakis.tech/medicalalarm/
Swift4から5に変えてみた結果
さて、Swift4から5に変えて見た結果ですが、54個のエラーが発生しました。
実は、エラーは何も起こらないと思っていたので、ファッ!?ってなりましたが、1つずつ見てみたら、半分以上は
UIControlState が UIControl.State
に変わったのに関係したエラーで
UIControlState.normal
->
UIControl.State.normal
UIControlEvents.touchUpInside
->
UIControl.Event.touchUpInside
という修正です。
省略形で .normal や、 .touchUpInside
と書いていた人はエラーが出ません。
今後のアップデートを見据えて、省略形で書ける所は全部省略形にしましょう!
その他のエラーも一気に共有していきます。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
->
// 全国民が通過する修正
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
// UIImageをNSDataに変換する関数
UIImagePNGRepresentation(image)
->
// わかりやすくて良いですね
image.pngData()
// プッシュ通知の音設定
UNNotificationSound.default()
->
// 関数からプロパティに変わった
UNNotificationSound.default
// UIActivityIndicatorView
_indicator?.activityIndicatorViewStyle = .whiteLarge
->
// 短っ
_indicator?.style = .whiteLarge
self.tableView.insertRows(at: [indexPath], with: UITableViewRowAnimation.bottom)
->
self.tableView.insertRows(at: [indexPath], with: UITableView.RowAnimation.bottom)
// ソフトウェアキーボードのハンドリング
userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue
->
// UIResponserの中にまとめたい所存
userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
// ソフトウェアキーボードの表示ハンドリング
notificationCenter.addObserver(self, selector: #selector(self.showKeyboard(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
->
// UIResponserの中にまとめたい所存
notificationCenter.addObserver(self, selector: #selector(self.showKeyboard(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
// ソフトウェアキーボードの非表示ハンドリング
notificationCenter.addObserver(self, selector: #selector(self.hideKeyboard(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
->
// UIResponserの中にまとめたい所存
notificationCenter.addObserver(self, selector: #selector(self.hideKeyboard(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
// UITableViewCellのスワイプ処理の管理
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
->
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// 文字のカスタマイズ
let att: [NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 17.0)]
->
// 神経質
let att: [NSAttributedString.Key: Any] = [NSAttributedStringKey(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 17.0)]
以上です!
過去にあったような破壊的変更は無く、パラメータの分類を変えたどうでもいい変更(個人の感想です)だけでした。
恐れずSwift5に変更しても良さそうです(^o^)
他のアプリもSwift5に変更したら追記していきます。
twitterでも毎日開発情報をつぶやいていますので、フォロー宜しくお願いします。
<a href=’https://jp.freepik.com/free-vector/ベクター画像_4188755.htm’>Designed by Macrovector</a>