Skip to content

Files

Latest commit

 

History

History
100 lines (41 loc) · 1.5 KB

force_unwrapping.md

File metadata and controls

100 lines (41 loc) · 1.5 KB

Pattern: Use of force unwrapping

Issue: -

Description

Force unwrapping should be avoided.

Examples of correct code:

if let url = NSURL(string: query)


navigationController?.pushViewController(viewController, animated: true)


let s as! Test


try! canThrowErrors()


let object: Any!


@IBOutlet var constraints: [NSLayoutConstraint]!


setEditing(!editing, animated: true)


navigationController.setNavigationBarHidden(!navigationController.navigationBarHidden, animated: true)


if addedToPlaylist && (!self.selectedFilters.isEmpty || self.searchBar?.text?.isEmpty == false) {}


print("\(xVar)!")


var test = (!bar)


var a: [Int]!


private var myProperty: (Void -> Void)!


func foo(_ options: [AnyHashable: Any]!) {

Examples of incorrect code:

let url = NSURL(string: query)↓!


navigationController↓!.pushViewController(viewController, animated: true)


let unwrapped = optional↓!


return cell↓!


let url = NSURL(string: "http://www.google.com")↓!


let dict = ["Boooo": "👻"]func bla() -> String { return dict["Boooo"]↓! }


let dict = ["Boooo": "👻"]func bla() -> String { return dict["Boooo"]↓!.contains("B") }


let a = dict["abc"]↓!.contains("B")


dict["abc"]↓!.bar("B")


if dict["a"]↓!!!! {


var foo: [Bool]! = dict["abc"]↓!


context("abc") {
  var foo: [Bool]! = dict["abc"]↓!
}


open var computed: String { return foo.bar↓! }

Further Reading