Learn how to create a SwiftUI data model and work with local data in Swift in Xcode editor - Part 3

Опубликовано: 12 Октябрь 2024
на канале: Credo Academy
4,898
82

🟢 LIMITED TIME OFFER - SAVE 37%
Practical iOS and macOS app development tutorials with SwiftUI in 4K
https://credo.academy

In this SwiftUI lecture, we’re going to cover the basics of data. First, we will create a data model, then create a local data file that will store all of the information about the fruits in this encyclopedia app.

CHAPTERS

00:00 Introduction
00:12 Watch the demo of the iOS app
00:39 Create the data model with the Swift programming language
01:00 Create a new group and file for the model
04:45 Get familiar with the data file
05:06 Create a new data file in Swift 5
08:53 Intend the code lines
09:13 Copy and past the rest of the data from a text file
10:18 Data integration with SwiftUI framework
11:44 Create a new fruit property
12:10 Fix the error in the SwiftUI Preview
13:49 Add dynamic data for the image view
14:10 Add dynamic data for the title view
14:37 Add dynamic data for the headline view
15:30 Make the background gradient color dynamic
16:31 Play with different data in the Preview window
16:49 Fetching data with SwiftUI
17:42 Modify the For Each with a new closed-range loop
18:27 Add dynamic data to the Fruit Card View
18:57 Add dynamic data to the Preview as well
19:10 Test the application with the Live Preview
19:31 Test the iOS 14 app with Xcode's Simulator
20:02 Conclusion

Second, we will learn how to integrate and fetch this data as well. By the end of this class, we will display different fruits in the onboarding screen and make it more interesting. All right, without further ado, lets launch Xcode and start coding, shall we?

DATA MODEL

Our first task is to create a data model for all of the fruits. This data model is basically a blueprint that describes all kinds of information about the type of properties that need to be represented.

Having said that, right-click or control-click on the main project name on the Project Navigator. After that, let’s create a new group folder for this data model as I show you.

Give it the name: Model and let us create a new file for it. Select the ’Swift File’ from the available templates and name it to the ‘Fruit Model’.

Make sure that the destination is still the recently created ‘Model’ folder then click on the Create button. Xcode will open this file immediately.

Now, replace the default `Foundation` framework with the `SwiftUI` as I show you. Then enter a short but yet informative comment about this model.

TESTING

As usual, it’s time to test this application in the Simulator or on a real test device. Let’s build and run the Fruits iOS 14 project.

Of course, testing on the iPhone is much better than anything else in my option. Shortly after the app is launched, we can test how the onboarding screen is working with displaying all of the cards inside its Page Tab View.

What do you think about it?

I hope that you enjoyed developing it. In the next lecture, we will add more functionality so when a user taps on the Start button it will exit the Onboarding and enter the next screen.
8. Until then, happy coding and see you at the class!

#SwiftUI #Xcode #iOSDevelopment