forced-unwrapping

Swift safely unwrapping optinal strings and ints

。_饼干妹妹 提交于 2020-06-24 12:17:12
问题 When I am about to fire my segue for the 2nd view I also send some values like this: if let aTime = ads[indexPath.row]["unix_t"].int { toView.time = aTime } if let aTitle = ads[indexPath.row]["title"].string { toView.title = aTitle } In the second VC I have declared the varibles like: var time: Int? var title: String? and this is how I unwrap the values: if time != nil { timeLabel.text = String(time!) } if title != nil { titleLabel.text = title! } This all works I never get any error caused

Why using implicit unwrap or force unwrap letting the app crash at some stage not beneficial?

青春壹個敷衍的年華 提交于 2020-01-05 04:22:06
问题 My point is at some places we know that the variable won't have nil at all but due to some reason we can't instantiate it in the class's init function so we have to make it optional. I also know that we can use optional binding or guard techniques to get rid of it easily. But letting the app crash at some really stupid mistakes because of the implicit unwrap/force unwrap is beneficial for the developers at the developing phase in my opinion . My example would be: class

Purpose of forced unwrapping

走远了吗. 提交于 2019-12-21 17:19:25
问题 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.”

Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals

大兔子大兔子 提交于 2019-12-20 08:49:29
问题 I was very confused about forced unwrapping and implicit unwrapping at first. Now, the following understanding comes from my self-study: There is no action available for implicit unwrapping, but there is something called implicitly unwrapped Optionals. Implicitly unwrapped Optionals and normal Optionals are both Optionals, the difference being when accessing an implicitly unwrapped Optional, you confidently know that there is a valid value under the hood and ready for use. Normal Optionals

Why does Swift 2 favor forced unwrap over optionals?

南笙酒味 提交于 2019-12-20 06:38:12
问题 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? 回答1: 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

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

巧了我就是萌 提交于 2019-12-20 05:23:42
问题 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

When should you use assertions and preconditions and when you can use guard statements, forced unwrapping and error handling? [closed]

浪子不回头ぞ 提交于 2019-12-13 10:38:26
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . I've already read Difference between “precondition” and “assert” in swift. But still can't draw a clear line between the (different ways of unwrapping ie guard & ! + error handling) vs assertions. If I want my application to no longer work, can't I just force unwrap something

How to avoid Firebase fatal error: unexpectedly found nil while unwrapping an Optional value?

强颜欢笑 提交于 2019-12-13 01:14:43
问题 I am trying to read from Firebase and set the label to the value I get This is what the top of my ViewController class looks like: import UIKit import Firebase class ProfileTabViewController: UIViewController { //MARK: Properties @IBOutlet weak var nameLabel: UILabel! var userName = "userName" I have a Firebase Reference at var currentUserName = Firebase(url: "https://buzzmovieios.firebaseio.com/users/884962b7-9fd8-49db-b172-1ad7cb1414f4/Name") The random string is the uid returned by

How to understand `!` and `?` in swift? [duplicate]

▼魔方 西西 提交于 2019-12-11 05:39:34
问题 This question already has answers here : What is the difference between String? and String! (two ways of creating an optional variable)? (7 answers) Closed 2 years ago . I'm new in swift.When i declare a variable,or when i get a property of an instance,I find "!" and "?" is everywhere.Doing such things in Objective-C is quite easy,you can even not know the type of class with the "id" type.Why do we need the ! and the ? ? I want to know the reason of designing ! and ? ,instead of how to use

Early return/golden path in Swift

╄→гoц情女王★ 提交于 2019-12-07 11:14:48
问题 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