前提
Target: iOS9
Swift: 3.0
質問
自分のiOSやSwiftに対する理解が足りてないと思い質問です。
Swift2からXCodeのコンバータでSwift3へ変換を行うと、tableview関連のメソッドの引数がNSIndexPath
からindexPath
に変わりました。↓
Swift2
swift
1override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 2 switch indexPath.section {
Swift3(XCodeコンバータで変換)
swift
1override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 switch (indexPath as NSIndexPath).section {
この時indexPath as NSIndexPath
でキャストをしているのですが、可読性が下がるので、indexPath
だけにしました。↓
Swift3(NSIndexPathへのキャストをやめる)
swift
1override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 switch indexPath.section { // <- キャストやめる
これで、問題なくコンパイルもでき動作も正常なのです。
ですが、indexPath
のドキュメントにはiOS10+
と記載されているのでiOS9
では動かないのでは?と疑問に思いました。
やはりキャストはしておいた方がよいのでしょうか。それとも、キャストしないでも問題ないのでしょうか。
###試したこと
手元のiOSシミュレータiOS9.3
で正常に動きました。
###参考
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/07 05:03