This workshop will be retired on May 31, 2020.
Nullability Annotations in Objective-C8:28 with Amit Bijlani
Nullability annotations allow you to express your intent when defining an interface. It makes APIs clearer and reduces the need for documentation by specifying nil behavior. No more implicitly unwrapped optionals and have only explicit non-nil references, and simple optionals.
Hello my name is Ahmed, an iOS teacher and developer. 0:00 So you're an iOS developer, and are presented with a legacy code base. 0:04 Nowadays a legacy code base in iOS, means that you have to deal with Objective C. 0:09 Of course probably by now, you know that Swift and 0:15 Objective C can be in the same project. 0:17 The treehouse iOS app was created prior to Swift so, when I started 0:19 working on it last year, I wanted to rewrite the entire application in Swift. 0:24 But that was not a valuable option. 0:29 So we came to a compromise, any new code would be written in Swift. 0:31 Each new feature was written in Swift, while still supporting and 0:36 maintaining Object C. 0:39 We quickly realized the drawbacks of having to maintain two languages, 0:42 within the same application. 0:46 The biggest advantage of Swift is that it's safe. 0:49 What does that mean to be safe? 0:52 It means that undefined or unintended behavior, must be avoided at all cost. 0:54 And developer mistakes should be caught before software is in production. 1:01 One of the biggest flaws of Objective C. 1:06 Is that it allows for nose, or nose all over the place. 1:08 So, Charles Antony Richard, the inventor of the no reference said. 1:12 Said at a conference that this was his billion dollar mistake. 1:16 Why? 1:21 Because it led to enumerable errors, vulnerabilities and system crashes. 1:22 which have probably caused a billion dollars worth of pain and 1:27 damages in the last 40 years. 1:32 In Objective C, if you declare a property or a variable, 1:34 the compiler never complains that it has not been initialized with a value. 1:37 Now with Swift this is by design. 1:41 If you want to have a variable or 1:44 a property that can be null, then you have to denote it as an optional. 1:46 Which means that it can either have a value, or it can be null. 1:51 An optional, can be safely unwrapped to retrieve its value. 1:55 This prevents your app from crashing, during runtime. 1:59 Thankfully Apple has learned these lessons and try to bring over some of 2:03 these safety features, to Objective C with nullability annotations. 2:08 Let's say you have an Objective C. 2:13 application and you want to write Swift. 2:14 This application has a user model which looks, something like this. 2:17 If this Objective-C class, belonged to a project that contained both Object C and 2:21 Swift, then it would input this class into Swift as the following. 2:27 The username and password properties, will both be marked as implicitly unwrapped. 2:33 and so will the parameters, to the login method. 2:38 Why, because Swift is safe and 2:41 does not allow for nodes unless you mark something as an optional. 2:43 With nullability annotations, 2:47 we instruct the compiler whether or not you want to allow for nodes. 2:49 Now we will mark the user name property as no null, 2:54 because we want to make sure that it'll never contain a nil. 2:58 Since the password will be optional, we will mark it as nullable. 3:01 In addition, we will mark the parameters of the method as nonnull. 3:05 Which means, you cannot pass in no as an argument. 3:10 Notice the new keywords, nonnull and nullable. 3:13 Nonnull, means that the property cannot contain a null or a nil. 3:17 Nullable, means that the property may contain a no or a value. 3:22 In swift terms, that will be an optional. 3:27 These annotations, allow you to express your intent when defining an interface. 3:30 And makes APIs clearer and reduces the need for 3:35 documentation, specifying no behavior. 3:38 When used from Swift code, it allows the interface to transform as needed, 3:42 to express nullability in Swift terms. 3:47 So no more implicitly on dropped optional. 3:51 Will have to explicitly define no references and simple optionals. 3:54 But you need to understand that the language Objective C hasn't changed, 4:00 and these annotations don't change the runtime behavior of the code at all. 4:04 In other words, the generated code with and 4:09 without annotations, is basically the same. 4:12 But the compiler comes to our rescue, and 4:16 will generate warnings when the API is misused. 4:18 One other addition to these annotations, are regional markers. 4:22 In our code, since the only thing marked as nullable is the password. 4:27 We can use regional markers, that will declare entire blocks of code. 4:31 Within a particular region to be marked as nonnull or null. 4:35 Now we no longer need to individually mark properties or parameter, is as non-null. 4:40 However, we can still mark the password as null-able with and that region. 4:46 The regional markers, help us declare huge blocks of code for null-ability, 4:51 instead of marking them individually. 4:56 We have four null-ability annotations available to us. 4:59 Null_unspecified, which bridges to a Swift implicitly unwrapped optional. 5:03 This is the default. 5:09 We notice that when we declare our user model without annotations, 5:10 it imported into Swift as implicitly unwrapped. 5:15 Nonnull, the value won't be nil it bridges to a regular reference. 5:19 Nullable the value can be nil, it bridges to an optional. 5:26 Nonnull but resettable. 5:31 The value can never be nil, when read but you can set it to know to reset it. 5:34 This only applies to properties. 5:40 For the C pointers, we have the underscore variant and Objective C properties. 5:43 Have the simplified annotation, which makes it easier to read. 5:47 Going back to our previous example of the user model, 5:52 it's not sufficient that we return a boolean value. 5:55 to indicate whether the user logged it was successful or not. 5:58 We need to provide an error with a description. 6:02 In Objective C. 6:05 we do this by providing an error pointer. 6:06 The two asterisks, denote that it is a pointer to a pointer. 6:09 In Swift, the compiler recognizes that we want to perform error handling and 6:14 changes the method signature, to throw an error. 6:19 But you will notice, that it still takes an error parameter. 6:22 We can suppress that parameter by changing the method signature for 6:26 Swift using the N S swift name annotation. 6:29 This ends up creating a more Swifty API, or in other words 6:34 it creates a method signature that falls more in line with the Swift guidelines. 6:38 And before we move on, you will get a compiler warning for the error parameter. 6:43 The warning will say, pointer is missing nullability type specifier. 6:48 To suppress this warning, you will have to specify, 6:53 whether the error pointers can be nullable. 6:56 Using the underscore variant of the annotation. 6:58 Now we have two methods, in our user model. 7:02 One that takes in an error pointer, and one that doesn't. 7:05 For Swift, the second version of our method is good enough. 7:09 Because it will throw an error in case the log in failed. 7:13 Probably to support our Objective C code, we have two variations of this method. 7:16 We can suppress the first version, 7:21 when using our Swift code by using the NS-Swift and Unavailable macro. 7:24 Our final generated Swift interface, 7:30 looks clean as if it were originally written in Swift. 7:32 As you will notice, that our Objective C interface is messy. 7:37 But our generated Swift interface is clean. 7:40 Of course you want to add the Swift specific annotations. 7:43 Only if we wish to support both object C and Swift in your projects. 7:46 If you annotate your objective C, you'll get nice type bridging over in Swift. 7:51 And even if you don't add any Swift code, 7:56 these annotations will help with code completion. 7:58 Finally, the compiler will warn you. 8:01 For example, if you have a nonnull parameter and you try to pass it in nil. 8:04 So in short, it's good practice to start adding these annotations. 8:09 It will help you while writing Swift or 8:13 Objective-C, while making you a better iOS developer. 8:15 I know your coworkers will thank you for it. 8:19 As always, check out the teachers notes for further reading material. 8:22 Good luck and see you next time. 8:26
You need to sign up for Treehouse in order to download course files.Sign up