Swift-' as ', usage of ' as '

Source: Internet
Author: User


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 '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.