Swift Optionals: When to use if let, when ? and !, when as? and as

前端之家收集整理的这篇文章主要介绍了Swift Optionals: When to use if let, when ? and !, when as? and as前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Hi this isMarin- the author of Touch Code Magazine,I hope you are enjoying my tutorials and articles. Also if you need a bright iPhone developer overseas contact me- I do contract work. Here's my LinkedIn profile

It seems everybody is in love with Swift,but since Optionals are a rather advanced concept the Swift code I see published on the Internet tends to rather randomly unwrap Optionals. And to be honest I haven’t seen a good blog post to clearly explain in plain text when to unwrap in what way. So I thought I’d write one today.

So let’s start by a quick review what Optionals are.

What are Optionals

Consider your plain old data types just as the one you used in your Objective-C (or PHP,Javascript,etc.) code. Let’s say you have two variables calledageandheight. They are of the simple typeIntand can hold any integer number. Let’s see a quick code sample:

You declare a variableageby using the keywordvarand assign the value35. You might change your mind later on and assign another value,say36,and do that again and again.heighton the other side is a constant – once you assign the value100to it – that’s it!

In both cases however you know thatIntcontains a value – an integer number. What youcannotdo is to assignnothingto that variable –nil.

For example if those pieces of data represent an employee profile in a company database,for some records you might not know the height of the employee – in that case you shouldn’t set a value of0– you really should rather set the value tonothingormissing.

The case above would be a case where you would like to use anOptionaldata type.

Optionalin fact,what many people don’t realize,is just an enumeration defined in Swift’s standard library and topped with some Syntax sugar. The enumeration looks like this (abbreviated):

SoOptionalcan either containNone– and sinceOptionalis NilLiteralConvertible you can guess that that’s the case for when you want to use nil for anOptionalvalue. The latter case defines thatOptionalcan hold a value of type T – any type T you’d like.

So for an Optional<Int> – it can contain either nil or the Int value. Very simple and easy. So let’s use Swift’s Syntax sugar?and declareageandheightvariables that can hold and integer number or a nil.

By adding a?immediately after the data type you tell the compiler that the variable might contain a number or not. Neat! Notice that it doesn’t really make sense to define Optional constants – you can set their value only once and therefore you would be able to say whether their value will be nil or not.

To make it crystal clear what’s going on when you use Optionals let’s look at what’s going on behind the scenes in the above example. The line:

is actually equivalent to:

It looks very easy once you know what’s going on there,right? The?symbol you add to datatypes to declare them as Optinal is just Syntax sugar that does translates into the code above.

Okay,now that I hope you have a good idea what an Optional data type is,let’s have a look at how to use those types in code.

When to use ? and when !

Let’s imagine you have a simple iPhone app – a UIKit based application. You have some code in your view controller and you want to present a new view controller on screen on top of your existing one. You decide to push the new one on screen via a navigation controller.

As you,hopefully,know every ViewController instance has a propertynavigationController. If you are building a navigation controller based app this property of your app’s master view controller is set automatically and you can use it to push or pop view controllers. If you use a single app project template – there won’t be a navigation controller created automatically for you,so your app’s default view controller will not have anything stored in thenavigationControllerproperty.

I’m sure you already guessed that this is exactly a case for anOptionaldatatype. If you check UIViewController you will see that the property is defined as:

So let’s go back to our use case. If you knowfor a factthat your view controller will always have a navigation controller you can go ahead andforce unwrapit:

When you put a!behind the property name you tell the compilerI don’t care that this property is optional,I know that when this code executes there always will be a value store so treat this Optional like a normal datatype. Well isn’t that nice? What would happen though if thereisn’ta navigation controller to your view controller? If you suggestion that there always will be a value stored in navigationController was wrong? Your app will crash. Simple and ugly as that.

So,use!only if you are 101% sure that this is safe.

How about if you aren’t sure that there always will be a navigation controller? Then you can use?instead of a!:

What the?behind the property name tells the compiler isI don’t know whether this property contains nil or a value,so: if it has value use it,and oterwise just consider the whole expression nil. Effectively the?allows you to use that property just in the case there is a navigation controller. Noifchecks of any kind or castings of any sort. This Syntax is perfect when you don’t care whether you have a navigation controller or not,and want to do something only if there is.

Perfect! But how about if you want to do somethinge else in case there wasn’t a navigationController? For example show an alert Box to tell the user something.

Then you would use anif letexpression.

When to use “if let”?

if letis a special structure in Swift that allows you to check if an Optional holds a value,and in case it does – do something with the unwrapped value. Let’s have a look:

Theif letstructure unwraps controller.navigationController (i.e. checks if there’s a value stored and takes that value) and stores its value in thenavconstant. You can usenavinside the first branch of theif. Notice that inside theifyou don’t need to use?or!anymore. It’s important to realize thatnavis actually of typeUINavigationControllerthat’s not an Optional type so you can use its value directly. (If you remember in contrast the navigationController’s original type isUINavigationController?)

if letis useful also in another case. Imagine that instead of a single method call you want to do more stuff with your navigationController. If you do not useif letyou code will looke like this:

This code works but you need to unwrap the property 4 times and the code gets quite repetitive. It’s much cleaner and nicer (and faster) to unwrap the value once in anif let:

Much nicer,isn’t it?

One final note –navin the above example is a constant. You can change properties on it and call methods,but you can’t change the value ofnav. It’s worth mentioning that you can do something like that by simply usingif var. It works the same way,but instead of an unwrapped constant you have an unwrapped variable and you could change its value if you wanted to so.

This brings us to the final use case I wanted to cover when using Optional data types – type casting.

When to use as? and when as

Let’s continue working with our imaginary UIKit app. Let’s assume you present a new modal view controller on screen (e.g by using presentViewController(_,animated:,completion:) ).

You define a custom view controller,which has an extra property calledlastUpdated. Let’s suppose you wanted to change this property once the controller is presented. You can access the presented controller via thepresentedViewControllerproperty on the main view controller:

presentedViewControlleris of Optinal type UIViewController? so you can easily unwarp the value and use it. But how about if you want to change thelastUpdatedproperty,which you define inMyViewControllerand is not defined in UIViewController?

You can cast an Optional property by usingas?like so:

as?triesto cast the value to the given type and if it doesn’t succeed returns a nil. That’s why the resulting type is always an optional value. You are forced to useas?also when the cast is not guaranteed to succeed – for example if you are trying to cast an AnyObject or Any value to a concrete class.

So,long story short,useas?if the cast could fail in any possible way.

As opposite to a possible failing cast when you useas?there’s also the cases where you are 101% sure that the cast will succeed and in those cases you can omit the ? and just cast by usingas.

Let’s have a look as couple of examples when casting withasis useful.

Consider you are calculating the number of sheep on a farm. Through some weird math you get a fractional value,like in this example:

Now sheepCount is aDoubleand contains the value 35.5. Since you need an integer number and since casting from a Double to an Int is safe (e.g. will alwaus succeed by cutting off the floating part of the number) you can just cast the result to anIntlike so:

In this case since the expression is casted to an Int type – also the sheepCount constant type will be Int (in contrast in the prevIoUs example sheepCount was of type Double)

Another case forasis when you are matching a pattern in aswitchstatement. In aswitchyou can always useasinstead of aas?since the pattern is matched only when the cast succeeds. For example if you didn’t know what typesheepCountwas – you can check that by passing it to a switch statement and try casting to different types in the different cases:

In each of thecasestatements the local,for that case,constant calledsheepis of a different type – in the former case it’s of typeIntand in the latterDouble.

Where to go from here?

I hope that was a detailed introduction to how to use Optionals. Remember that is important not only for your code to work,but also for you to know how does it work

If you are interested to learn more about everything that has to do with Swift,consider reading my colleagues Colin Eberhardt and Matt Galloway’s book “Swift by Tutorials“.

By the way Swift by Tutorials is part of the iOS8 Feast where you can send a single tweet and have a chance to win some of the prizes in a total value of over $10,000. Read more here –iOS8 Feast.

Thanks for reading and I’ll see you next time.

The post was originally published on the following URL: http://www.touch-code-magazine.com/swift-optionals-use-let/

·

原文链接:https://www.f2er.com/swift/323541.html

猜你在找的Swift相关文章