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?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
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:
Perhaps what you ment was that Xcode seemingly complains less often when you actually do force unwrap optionals, or, as a bad habit of Xcode, prompts you to force unwrap things to avoid compile time errors. This is generally because Xcode cannot know at compile time that you just wrote code that will break your app at runtime.
Xcode may seemingly at times have only one single purpose with its "smart hints": namely to absolve compile time errors. If you try to assign the value of a
String?
type (optionalString
) toString
type, Xcode will prompt you with a compiler error and ask if you ment to add the forced unwrapping operator!
. Smart Xcode, you say? Meh, Xcode is good for many things, but deciding how you unwrap your optionals is, not yet anyway, one of them. So even with Xcode prompting you for all kinds of things: if you can use optional chaining, do.There might be exception, of course. For the controller part of the MVC design pardigm, you usually use the
as!
operator to do "forced conversion" (casting), with Xcode sometimes telling you to explicitly to useas!
instead ofas
, e.g. "Did you meanas!
... ?". In these situations, Xcode can sometimes actually know what its doing and infer that you are trying to cast, as an example, a customUIViewController
class instance to typeUIViewController
, i.e., to it's parent class. I'd say that this is perhaps one of the few times I use the "forced" marker!
without go-arounds; forced conversion to types I know with 100% certainty to be castable.But let's leave the subject of type conversion/casting and move on to optional types, wrapping, and optional chaining, in general.
Generally, you should avoid force unwrapping unless you explicitly knows that the entity you unwrap will be non-nil. For general class properties, variables and so on, given that you state this question the way you do, I'll give your the following advice:
Below follows an example of the dangers of forced unwrapping. You can treat the first
if
clause (if arc4random...
) as any smaller or larger segment of some program you've written with imperative programming techniques: we don't really know in detail just how 'name' will turn out until runtime, and our compiler can't really help us out here.I recommend you to read up on optional chaining, a key subject in Swift.
do you mean that cocoa stuff? do you mean implicitly unwrapped?
The protocol to which all classes implicitly conform.
When used as a concrete type, all known @objc methods and properties are available, as implicitly-unwrapped-optional methods and properties respectively, on each instance of AnyObject. For example: