SwiftUI Crash Course
The course is part of this learning path
This course delves into SwiftUI, which you can use to create user interfaces for your apps. We're going to cover how to use it, how to create views, and how to leverage it to create responsive apps that work in all screen sizes.
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi, within this lecture, we're going to finalize our app by passing information from this ContentView to the DetailsView so that we can use this model's properties in order to display the image and other information. And best way to do that is to create a variable inside of our DetailsView so that we can get the chosen model. So, I'm going to call this (chosenFavoriteElement) and this will be a type of FavoriteElements, okay? So, this will ask us to give a FavoriteElements instance when we try to initialize DetailsView. So, when we do that, whenever we write DetailsView, it will ask us to choose a FavoriteElement. So now, I did that and if I do a command B to build this up, so I am getting some weird error. So, let me do a command B. And as you can see, I'm even getting that error under this preview struct. So, we haven't talked about this yet, but this displays the preview, okay? So, this defines what will be displayed in the preview on the right-hand side. As you can see now, we are displaying the DetailsView struct over here. And since we are initializing this DetailsView, it's asking for the chosenFavoriteElements. So, if we give some examples, chosenFavoriteElement over here, it will display as an example on the right-hand side. For example, let's give the Dark Knight as an example. Of course, this won't affect the real app itself, but it will only affect the preview. We will see the dark knight as an example in our preview once we do that, okay? So, let me try to resume this and let's try to build this and we get an error, and the reason for that error is because we are actually calling DetailsView inside of our content view as well. And this time navigation link destination is asking us to provide the same parameter to here so that we can pass the information, right? So, it's easy to get the chosenFavoriteElement right now because it's already given as an element here. So, if I write elements here, then it will pass this information to the DetailsView and so that I can come over here and get the chosenFavoriteElement and do whatever I want to do with that element. For example, let me wrap this inside of a VStack because we're going to have more than one text, for right now, I'm just going to display the name of the chosenFavoriteElement, and for example, this will just display the dark knight name because I'm using the dark knight as an example inside of my preview, right? But in the real app I'm not going to see the Dark Night all the time, I'm just going to see whatever I choose. So, let me resume this and let me try this from here, okay? Once it's loaded, you can click over here and you will see whatever you choose like this. So, as you can see, what I'm choosing is shown in the DetailsView. So, that's good. That's what I wanted. Now, all we have to do is just to come over to DetailsView and have the other views as well, like an image. So, when I write image, it will ask me for a name, and we already have that name stored in (chosenFavoriteElement.imagename, right? So , I can get this and display it on my screen. And of course, this is going to be too big, so I'm going to resize it as usual, and I'm going to give some aspect ratio to this image and I'm going to say that fit. And let's see, yep, this is good, but this is far too close to each other. So, maybe we have to make this a little bit bigger, okay? And let's see, yep, this doesn't work. Let me give this some padding, yep, this looks good. Now, we have to display the description as well. So, let me call Text(chosenFavoriteElement), yep, this one (chosen.FavoriteElement.description). So, let me give some padding over here or some other fonts as well. So, I believe the current font is good and current padding is even good. So, we can use the same DetailsView in every element that we have. Let me run this from the scratch, and here you go. We have the Metallica image, we have the Megadeth image and this looks good to me. So, let's try the others as well. So, this is a little bit big but it looks good. So, here you go now, we are done with our app. The good thing about it is that I can come over here and change whatever I want since I have only one view regarding this view, it will affect everything. So, if I'm not satisfied with the image size, for example, I can just change it for once and I will use it for every other view as well. So, this is cool. Now, you have learned a lot about SwiftUI but again there is a lot more to learn about SwiftUI as well. So, if you're ready you can go over here to developer.apple.com. And in the documentation, you can go for a SwiftUI and try to figure out yourself. If you're not ready to do that, I suggest you go on and check out our other course on SwiftUI so that maybe you can feel a little bit better when you try to learn about all these things. But, the Apple has a really good documentation regarding SwiftUI and they have this landmark book app that they are doing in SwiftUI. It's kind of the landmark book that we are doing in this course but it's poorly written in SwiftUI, and we believe SwiftUI will take its place in most of the Apple projects in a couple of years, because now there is a better way to create user interfaces and there is a better way to work with data and all other things. And people will choose it over main storyboards and other things in the upcoming years. Now, it's too early to switch all your projects to the SwiftUI, because it will be only effective as of iOS 13. So, it will take a couple of years at least. But keep in mind that this SwiftUI has a great feature ahead. So, let's stop here and continue within the next section.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.