When to use ' as? ' and ' as '
Let's continue to apply write-point code to our illusion of uikit. Suppose you need to play (show) a new modal view controller to the phone's screen (for example by using the Presentviewcontroller (_, Animated:, Completion:) method).
//your custom view controllerclass MyViewController: UIViewController { var lastUpdated: NSDate? = nil}//in your master view controllerlet myVC = MyViewController()presentViewController(myVC, animated: true, completion: nil)
You define a custom view controller that has an extra property called lastupdated . Let's just assume that you want this property to be modified when the controller comes in. You can get this out-of-the-game controller on the main view controller via the Presentedviewcontroller method:
controller.presentedViewController!.title = "New Title"
Presentedviewcontroller is the optional type of Uiviewcontroller? , so you can easily unpack it to get its value and use this value. But what if I want to use or change a lastupdated that is defined only in Myviewcontroller and not defined in Uiviewcontroller?
Can you pass as ? Convert such objects into optional types, such as this:
let myVC = controller.presentedViewController as? MyViewControllermyVC?.lastUpdated = NSDate()
As ? Attempts to convert the value to the given type, and returns a nil object when the process is unsuccessful. This is why the result of the output is always a optional value. When you cannot guarantee that the conversion succeeds you have to use as, for example you try to convert anyobject or any value into a real class (entity Class).
So, in short, use as when you may fail when converting types. This is very useful.
With a conversion error that may occur, use as ? The opposite is when you can 101% guarantee the conversion will succeed, you can delete? Use as directly.
Let's take a look at some practical examples of using as.
Think of a situation in which you are calculating the number of sheep in a farm. However, you get a result with a decimal number through some intricate mathematical methods, such as:
71/2.0
Now Sheepcount is a Double type that contains a value of 35.5. When you need an integer and the double type is safe to convert to the int type (it must be possible to successfully remove the floating-point part), you can convert the result to the int type as follows:
let sheepCount = 71/2.0 as Int
In this case, the object is converted to the int type, that is, the Sheepcount constant is converted to the int type (in contrast, in the previous example, Sheepcount is a double type).
Another about as situation is when you use the switch statement to do pattern matching. In switch mode there is, and only if the conversion is successful, will usually replace as with as ? For example, you don't know what type sheepcount is, you can detect its type through switch syntax and try to use the corresponding type in different situations:
switch sheepCount { case let sheep as Int: println("\(sheep) found") case let sheep as Double: println("oops - fractional sheep!") default: break}
In this case code, a constant named sheep is a very complex type-in the former case the Int type and in the latter case the Double type.
Swift-' as ', usage of ' as '