Is force cast really bad and should always avoid it?

前端 未结 7 1339
无人及你
无人及你 2020-12-13 09:11

I started to use swiftLint and noticed one of the best practices for Swift is to avoid force cast. However I used it a lot when handling tableView, collectionView for cells

7条回答
  •  余生分开走
    2020-12-13 09:52

    When you are working with your types and are sure that they have an expected type and always have values, it should force cast. If your apps crash you can easily find out you have a mistake on which part of UI, Dequeuing Cell, ...

    But when you are going to cast types that you don't know that is it always the same type? Or is that always have value? You should avoid force unwrap

    Like JSON that comes from a server that you aren't sure what type is that or one of that keys have value or not

    Sorry for my bad English I’m trying to improve myself

    Good luck

提交回复
热议问题