Developing iOS Apps with SwiftUI

lemaidiemhang

New member
## Phát triển các ứng dụng iOS với Swiftui

Swiftui là một khung mới mạnh mẽ từ Apple giúp bạn dễ dàng tạo ra các giao diện người dùng đẹp và tương tác cho iOS, iPados, MacOS và TVOS.Đó là một khung khai báo, có nghĩa là bạn mô tả những gì bạn muốn giao diện người dùng của bạn trông như thế nào và Swiftui đưa ra cách để biến nó thành hiện thực.Điều này giúp Swiftui dễ sử dụng hơn nhiều so với các khung truyền thống như UIKIT, yêu cầu bạn viết nhiều mã để tạo ra các giao diện người dùng đơn giản.

Nếu bạn chưa quen với phát triển iOS hoặc nếu bạn chỉ đang tìm kiếm một cách đơn giản hơn để tạo giao diện người dùng, thì Swiftui là một lựa chọn tuyệt vời.Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách bắt đầu với Swiftui bằng cách tạo một ứng dụng đơn giản hiển thị danh sách các bộ phim.

### Điều kiện tiên quyết

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau:

* Mac chạy macOS 11 trở lên
* XCODE 13 trở lên
* SDK iOS 15

### Tạo một dự án mới

Để tạo một dự án mới trong xcode, hãy mở menu ** tệp ** và chọn ** mới **> ** Dự án **.Trong cửa sổ ** loại dự án **, chọn ứng dụng ** ** và sau đó nhấp vào ** Tiếp theo **.Trong trường ** Tên **, nhập tên cho dự án của bạn, chẳng hạn như "Phim".Trong trường ** nền tảng **, chọn ** ios **.Trong trường ** ngôn ngữ **, chọn ** swiftui **.Bấm tiếp**.

Trong cửa sổ ** chọn một mẫu **, chọn mẫu ** đơn xem ** và sau đó nhấp vào ** Tiếp theo **.Trong trường ** vị trí **, chọn một vị trí trên máy Mac của bạn nơi bạn muốn lưu dự án của mình.Nhấp vào ** Tạo **.

### Tạo giao diện người dùng

Giao diện người dùng cho ứng dụng của chúng tôi sẽ bao gồm một danh sách các bộ phim.Để tạo danh sách, hãy mở tệp ** Main.storyboard ** trong ** Điều hướng dự án **.Kéo một danh sách ** ** Xem từ thư viện ** đối tượng ** vào cảnh ** Xem bộ điều khiển **.

Trong Trình kiểm tra ** thuộc tính **, đặt thuộc tính ** nội dung ** của danh sách ** ** Xem thành ** phim **.Điều này sẽ nói với Swiftui sử dụng cấu trúc `phim` làm nguồn dữ liệu cho danh sách.

Tiếp theo, chúng ta cần tạo cấu trúc `phim`.Mở tệp ** movie.swift ** trong ** Mã nguồn ** Trình chỉnh sửa.Struct `film` nên có các thuộc tính sau:

* `Tiêu đề`: Tiêu đề của bộ phim.
* `Được phát hành`: Ngày phát hành của bộ phim.
* `posterimage`: URL của hình ảnh poster của bộ phim.

Chúng ta cũng có thể tạo một thuộc tính được tính toán có tên là `isfavorite` trả về giá trị boolean cho biết liệu bộ phim có được yêu thích hay không.

`` `Swift
Phim cấu trúc {
Đặt tiêu đề: Chuỗi
Hãy phát hành: Ngày
Hãy để posterimage: URL

var isfavorite: bool {
lấy {
// Nhận phim yêu thích của người dùng từ UserDefaults.
Let favorites = userDefaults.standard.array (forkey: "yêu thích") như?[Sợi dây] ??[]

// Trả về đúng nếu tiêu đề của bộ phim nằm trong danh sách các mục yêu thích.
Trả về yêu thích.Contains (Tiêu đề)
}

bộ {
// Đặt tiêu đề của bộ phim trong danh sách các mục yêu thích.
UserDefaults.standard.set (yêu thích, forkey: "yêu thích")
}
}
}
`` `

### Liên kết dữ liệu với giao diện người dùng

Bây giờ chúng tôi đã tạo ra cấu trúc `movie`, chúng tôi cần liên kết nó với giao diện người dùng.Để làm điều này, chúng ta cần tạo một struct `filmviewModel` sẽ quản lý dữ liệu cho danh sách.

Struct `filmviewModel` nên có các thuộc tính sau:

* `Phim`: Danh sách các đối tượng` Phim`.
* `isfavorited`: Một giá trị boolean cho biết liệu bộ phim hiện tại có được yêu thích hay không.

Struct `filmviewModel` cũng nên có một phương thức gọi là` fetchmovies () `có được một danh sách các bộ phim từ internet.

`` `Swift
cấu trúc
=======================================
## Developing iOS Apps with SwiftUI

SwiftUI is a powerful new framework from Apple that makes it easy to create beautiful and interactive user interfaces for iOS, iPadOS, macOS, and tvOS. It's a declarative framework, which means that you describe what you want your user interface to look like, and SwiftUI figures out how to make it happen. This makes SwiftUI much easier to use than traditional frameworks like UIKit, which require you to write a lot of code to create even simple user interfaces.

If you're new to iOS development, or if you're just looking for a simpler way to create user interfaces, then SwiftUI is a great option. In this article, we'll show you how to get started with SwiftUI by creating a simple app that displays a list of movies.

### Prerequisites

To follow along with this tutorial, you'll need the following:

* A Mac running macOS 11 or later
* Xcode 13 or later
* The iOS 15 SDK

### Creating a new project

To create a new project in Xcode, open the **File** menu and select **New** > **Project**. In the **Project Type** window, select **App** and then click **Next**. In the **Name** field, enter a name for your project, such as "Movies". In the **Platform** field, select **iOS**. In the **Language** field, select **SwiftUI**. Click **Next**.

In the **Choose a template** window, select the **Single View App** template and then click **Next**. In the **Location** field, select a location on your Mac where you want to save your project. Click **Create**.

### Creating the user interface

The user interface for our app will consist of a list of movies. To create the list, open the **Main.storyboard** file in the **Project navigator**. Drag a **List** view from the **Object library** onto the **View controller** scene.

In the **Attributes inspector**, set the **Content** property of the **List** view to **Movie**. This will tell SwiftUI to use the `Movie` struct as the data source for the list.

Next, we need to create the `Movie` struct. Open the **Movie.swift** file in the **Source code** editor. The `Movie` struct should have the following properties:

* `title`: The title of the movie.
* `releaseDate`: The release date of the movie.
* `posterImage`: The URL of the movie's poster image.

We can also create a computed property called `isFavorite` that returns a boolean value indicating whether the movie is a favorite.

```swift
struct Movie {
let title: String
let releaseDate: Date
let posterImage: URL

var isFavorite: Bool {
get {
// Get the user's favorite movies from the UserDefaults.
let favorites = UserDefaults.standard.array(forKey: "favorites") as? [String] ?? []

// Return true if the movie's title is in the list of favorites.
return favorites.contains(title)
}

set {
// Set the movie's title in the list of favorites.
UserDefaults.standard.set(favorites, forKey: "favorites")
}
}
}
```

### Binding the data to the user interface

Now that we have created the `Movie` struct, we need to bind it to the user interface. To do this, we need to create a `MoviesViewModel` struct that will manage the data for the list.

The `MoviesViewModel` struct should have the following properties:

* `movies`: A list of `Movie` objects.
* `isFavorited`: A boolean value indicating whether the current movie is a favorite.

The `MoviesViewModel` struct should also have a method called `fetchMovies()` that fetches a list of movies from the internet.

```swift
struct
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock