From the course: SwiftUI Essential Training

Swift review: Conditional operators and arrays

From the course: SwiftUI Essential Training

Swift review: Conditional operators and arrays

- [Instructor] If you're relatively new to Swift, there's a few things I'd like to discuss about SwiftUI and Swift, especially if you're coming from UIKit. Unlike UIKit, SwiftUI heavily uses structs instead of classes for user interfaces. There's some behaviors you'll have to get used to with structs if you do everything in classes. More importantly, var in structs works very differently than in classes. As structs are value types, it is more difficult to change values of properties internally with var. You'll find var works a lot more like let than you would expect. On the other hand, the same behavior means init is implicit. Any unassigned declared variable is implicitly a parameter when calling a struct. The body of Swift UIViews is a computed property that returns a view. They do not allow execution of basic Swift code inside the body. While there are places you can use if else, you'll run into places where even conditional code is hard to add. This is why the conditional operators will become a good friend of yours in SwiftUI. There's two conditional operators you should know. The ternary conditional operator is one of the most important. Let's say I wanted to change the Hello World message depending on the value of isPizza. I'd change it like this. I'd have some kind of Boolean value, and then I would have a question mark, and that question mark will evaluate the answer, sort of like an if and an if then else. And then the true answer would be right after the question mark, in this case Huli Pizza. And then the false one would be after the colon, in this case, Hello World. So what would happen in this case, because I have false, is I would end up with Hello World being printed. Now the other operator is the nil coalescing operator. If you have optional values, like I have here, with an image name that's set to nil, and you want to have a value show up for nil, the double question marks do the trick. So because we have nil, when I do print(presentedName), I get Placeholder. Now you'll be running into these on a regular basis, and it's good to keep familiar of how they work inside of SwiftUI, because you could throw them in as parts of your parameters. Now, there's also some array methods that you'd like to be familiar with too, when we start working with lists. So let's say we have an array that looks like this. It's going to be an array of a struct named Food, and I've got them all set down there. And I can use various methods on array to make these work. So for example, I have one called removeLast, and that does exactly what it says. I put removeLast after an array, and the last element of the array disappears. There's also removeAll, and removeAll also does what it says. If you put removeAll on an array, the array becomes empty. In the other direction, if I put append, and then I put an element for the array in the append, it will add that element in. In this case, Ice Cream. Contains(where:) checks if the element exists in the array. The where iterates through all elements, checking if a property of the element is true through the predicate closure. Predicate closures return bool values. When the condition is true, the contains is true. The $0 is the element. And here, I check the food property if there is one with Pizza, which in this case, there is. Array.first(where:) does the same as first, but returns an index instead. For example, if I'm looking for Pasta, I find the index is one. Now, one important thing to note here. What if I try the index of an Acai Bowl? I'm going to get nil. First and firstIndex are optionals. Remember to unwrap them. Now, for example, to change an element, I could do this code to remove and insert a food at the same index. I'm going to put it in if let so that I can unwrap the first index from the optional, and then I'll use that index to remove it from the array. And then I'll use that index to insert id: 42, food "Mochi" in the same space. That's the stuff we'll be using. If you want more information, check out other courses on Swift in the library for a deeper dive.

Contents