Rendow(走ると再生速度が変わるオーディオプレイヤー)をSwift 2.0 + Xcode 7に対応させました。
もともとSwift 1.2 + Xcode 6で作成したアプリでしたが、Swift 2.0 + Xcode 7に移行しました。
コンバートで、varからletへの書き換えや、errorを使っているところをtry~catchに書き換えられましたが、そのあと、実行してみると大量に警告、注意がでました。
以下、出て来た警告、注意とどのように対応したかを記しました。
Initialization of immutable value ‘h’ was never used; consider replacing with assignment to ‘_’ or removing it
変更前
let h = graphSize.height
変更後
//let h = graphSize.height
使っていない変数があったのでremoveしました
sectionIndexTitlesForTableView’ has different optionality than expected by protocol ‘UITableViewDataSource’
変更前
/// 右端indexで表示される頭文字 func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]! { ... }
変更後
/// 右端indexで表示される頭文字 func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { ... }
型が~!(Implicitly unwrapped optional型)から、~?(Optional型)に変わっていたので変更
そのほか、Swift 1.2では~?(Optional型)になっていた引数が、Swift 2.0ではOptional型でなくなっていたりしたので、そこも修正
Cannot convert value of type ‘[NSObject : AnyObject]’ to expected argument type ‘[String : AnyObject]?’
変更前
let attr: NSDictionary = [NSFontAttributeName: font, NSForegroundColorAttributeName: color] let textSize:CGSize = text.sizeWithAttributes(attr as [NSObject:AnyObject])
変更後
let attr: [String : AnyObject]? = [NSFontAttributeName: font, NSForegroundColorAttributeName: color] let textSize:CGSize = text.sizeWithAttributes(attr)
directory not found for option ‘-F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk/Developer/Library/Frameworks’
~Testsに対してこの「ディレクトリが見つからない」というメッセージが出ていたので、対処
~Tests > Build Settings > Search Paths > Framework Search Paths
そこに
iphoneos/Developer/Library/Frameworks
があるのを削除しました
All interface orientations must be supported unless the app requires full screen.
「(全画面アプリにするのでなければ、)4つの向きに対応しなさい。」
これは、iOS9から登場したiPad用のSlide Over, Split View対応のためでしょう。
Adopting Multitasking Enhancements on iPad: Slide Over and Split View Quick Start
このAppleのドキュメントを読むと、”Supproted interface orientations (iPad)”キーに4つの向きを加えるとあり、iPhoneのほうは、向きを増やさなくても良い模様。
いったんは、すべての向きに対応して、iPad用をSplit Viewに対応しましたが、
iPad/iPhoneは、シェイクすると、テキスト入力のUndoができる | nackpan Blog
の記事に書いたように、揺れるとテキスト入力キャンセルダイアログが出てしまうので、結局Split View対応はやめることにしました。
・General > Deployment Info > Requires full screenにチェックして、全画面アプリとして対処しました。
これで、注意は消えました。
fatal error: swapping a location with itself is not supported
swift2 – fatal error: swapping a location with itself is not supported with Swift 2.0 – Stack Overflow
How do I shuffle an array in Swift? – Stack Overflow
自身とswapすることは、Swift 2.0では禁止とのことで
変更前
swap(&element[i], &element[j])
変更後
if i != j { swap(&element[i], &element[j]) }
自身とはswapしないように変更しました
基本的には注意メッセージにしたがって、書き換えていくという形で対処できました。
しかし、大量に警告・注意があったので、修正に相当時間がかかりました。