Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 1.1 KB

force_cast.md

File metadata and controls

27 lines (22 loc) · 1.1 KB

force_cast


Why was this rule disabled :


Although force casting is often a bad thing, there are a few places on iOS where it is commonly used and it is very handy sometimes.
Most of the time, it is used when casting UITableViewCells subclasses in tableView(_:, cellForRowAtIndexPath:), and no matter what the app wouldn't behave properly if the cell couldn't be dequeued, since this method excepts a UITableViewCell to be returned no matter what.
Another example is instantiating UIViewController subclasses from a UIStoryboard instance. If the view controller doesn't match the type it is force-casted too, the app won't behave properly either.

Examples


static var appDelegate: AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}
let cell = tableView.dequeueReusableCellWithIdentifier(FavoriteItemTableViewCell.cellReuseIdentifier, forIndexPath: indexPath) as! FavoriteItemTableViewCell
UIStoryboard(name: "iPadLists", bundle: nil).instantiateInitialViewController() as! iPadUserListsViewController