Quantcast
Channel: プログラミング雑記
Viewing all articles
Browse latest Browse all 70

swift エラーメッセージ - 型変換(Type Casting)

$
0
0
Cannot downcast from 'UIView' to a more optional type 'UITableViewHeaderFooterView?'
  • エラーが発生するコード
     iflet headerView = view as! UITableViewHeaderFooterView {
       ...
     }
  • 原因:viewがOptionalでないため、UITableViewHeaderFooterViewとして扱えない。
  • 修正
    let headerView = view as! UITableViewHeaderFooterView
    ...

Initializer for conditional binding must have Optional type, not 'UITableViewCell'
  • エラーが発生するコード
    iflet cell = sender as! UITableViewCell {
       ....
    }
  • 原因:senderがOptionalなのでUITableViewCellとして扱えない。
  • 修正
    iflet cell = sender as! UITableViewCell? {
        ....
    }
Cannot convert value of type 'Any?' to expected argument type 'UITableViewCell'
  • エラーが発生するコード
    iflet indexPath = self.tableView.indexPath(for: sender) {
        ...
    }
  • 原因:senderの型がAny?なのでキャストが必要。
  • 修正
    iflet indexPath = self.tableView.indexPath(for: sender as! UITableViewCell) {
        ...
    }




Viewing all articles
Browse latest Browse all 70

Trending Articles