forced-unwrapping

Early return/golden path in Swift

こ雲淡風輕ζ 提交于 2019-12-05 12:47:17
I'm used to write code with early return/golden path in Objective-C. I tried this approach in Swift, and noticed that early return comes at the expense of using the forced unwrapping operator ( ! ) when optionals are involved. Take a method that calculates the size of a directory. First, the golden path version: private func calculateSize_GoldenPath(directory:String) -> UInt64 { let fileManager = NSFileManager.defaultManager() var error : NSError? var contents = fileManager.contentsOfDirectoryAtPath(directory, error: &error) as [String]? if contents == nil { NSLog("Failed to list directory

Purpose of forced unwrapping

一世执手 提交于 2019-12-04 07:42:39
In swift documentation, you can find this : if convertedNumber != nil { println("convertedNumber has an integer value of \(convertedNumber!).") } // prints "convertedNumber has an integer value of 123." With this explaination Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. The exclamation mark effectively says, “I know that this optional definitely has a value; please use it.” This is known as forced unwrapping of the optional’s value: Ok, got it, but what's the usefulness of it ?

Why does Swift 2 favor forced unwrap over optionals?

旧时模样 提交于 2019-12-02 09:30:38
I no longer see Xcode complaining that certain things need optionals (the "?"). Now it is always forced unwrapped (the bang "!"). Is there any reason to use optionals anymore when we now have forced unwrap? I don't really know what you mean when you write that you no longer see Xcode complaining that "certain things need optionals. Now it is always forced unwrapped" . These two sentences contradict eachother: You may have non-optional variables as much as you wish, but optional can really nice once you get to know all the benefits of using them. If you have a property that is not an optional,

What is the difference when change to use optional chaining replace forced unwrapping in swift?

被刻印的时光 ゝ 提交于 2019-12-02 06:53:08
When call a function of an object instance, the object may be not exist(optional type), it seems like you can always put an question mark behind the object name, instead of put an exclamation mark behind the object name, and not crash. window!.rootViewController = containerViewController // forced unwrapping // Can change to question mark and not crash. window?.rootViewController = containerViewController // Optional chaining Is that in the place of use forced unwrapping, you can always change to use optional chaining, and the result is same? If yes, what is the difference? The difference is

Swift Optionals - Different ways of unwrapping

青春壹個敷衍的年華 提交于 2019-12-02 03:20:29
问题 I'll just get right to it: What's the difference between: var test: String? test = "this is an optional string" if test != nil { println("\(test!) IS NOT nil") } else { println("test is nil") } and if let test = test { println("\(test) IS NOT nil") } else { println("test is nil") } Both output the same results in a playground. I know implicitly unwrapping is not considered safe (in most cases) but, here I'm checking whether or not the values are nil before unwrapping? Are both methods valid

Difference between optional and forced unwrapping [duplicate]

别来无恙 提交于 2019-12-01 14:25:27
This question already has an answer here: What is an optional value in Swift? 13 answers Below is the code for optional string for variable name yourname and yourname2. Practically what is difference between them and how forced unwrapping in case of yourname2 var yourname:String? yourname = "Paula" if yourname != nil { println("Your name is \(yourname)") } var yourname2:String! yourname2 = "John" if yourname2 != nil { println("Your name is \(yourname2!)") } The String? is normal optional. It can contain either String or nil. The String! is an implicitly unwrapped optional where you indicate

Difference between optional and forced unwrapping [duplicate]

╄→гoц情女王★ 提交于 2019-12-01 14:04:23
问题 This question already has answers here : What is an optional value in Swift? (13 answers) Closed 2 years ago . Below is the code for optional string for variable name yourname and yourname2. Practically what is difference between them and how forced unwrapping in case of yourname2 var yourname:String? yourname = "Paula" if yourname != nil { println("Your name is \(yourname)") } var yourname2:String! yourname2 = "John" if yourname2 != nil { println("Your name is \(yourname2!)") } 回答1: The

How is a return value of AnyObject! different from AnyObject

做~自己de王妃 提交于 2019-11-30 10:46:59
The NSMetadataItem class in the Cocoa framework under Swift contains the following function: func valueForAttribute(key: String!) -> AnyObject! I'm still learning the difference (and details) between forced unwrapping and optional chaining. In the above function, does this mean: The key parameter must have a value, and The return value is guaranteed to have a value? My primary concern is with the exclamation point following the return value - once I have assigned the return value: var isDownloadedVal = item.valueForAttribute(NSMetadataUbiquitousItemIsDownloadedKey) Do I need to include an if

How is a return value of AnyObject! different from AnyObject

梦想与她 提交于 2019-11-29 15:38:28
问题 The NSMetadataItem class in the Cocoa framework under Swift contains the following function: func valueForAttribute(key: String!) -> AnyObject! I'm still learning the difference (and details) between forced unwrapping and optional chaining. In the above function, does this mean: The key parameter must have a value, and The return value is guaranteed to have a value? My primary concern is with the exclamation point following the return value - once I have assigned the return value: var

What does an exclamation mark mean in the Swift language?

青春壹個敷衍的年華 提交于 2019-11-25 22:59:58
问题 The Swift Programming Language guide has the following example: class Person { let name: String init(name: String) { self.name = name } var apartment: Apartment? deinit { println(\"\\(name) is being deinitialized\") } } class Apartment { let number: Int init(number: Int) { self.number = number } var tenant: Person? deinit { println(\"Apartment #\\(number) is being deinitialized\") } } var john: Person? var number73: Apartment? john = Person(name: \"John Appleseed\") number73 = Apartment