SwiftUI, in practice, part 1

0


With SwiftUI of some of the concepts that are inherent to the design of the user INTERFACE, AppKit, UIKit, and the WatchKit changes. This is the primary, is the fact that SwiftUI is dependent on a declarative, rather than imperative Syntax. At the time of the build, so the Views through the invocation of methods, and to control their behavior, it SwiftUI a Status on the use. It is important for the appearance and the functionality of a View is responsible for. It will change, it also provides for the automatic renewal of your View.

To work with the SwiftUI, and the basic concept in detail to explain the present, this article will show you a complete implementation of a small example-App for iOS-Based SwiftUI. This is a simple note management, notes, a title, and the content you create, and edit. In addition to this, there is an Option to notes, and later, as one of the favorites to score.

The implementation is described in the Following example, in order to understand it, he doesn’t need anything more than the latest Version of Xcode. First of all, create a new iOS project based on the “Single View App”template, and give it a name. Important: in addition to the programming language-Swift-should-be in the area of “User Interface” to the point “SwiftUI” is selected (see Fig. 1).

In Xcode, a new project is right at the Base of the SwiftUI up Fig. 1).

Before we get into the Detail to work with SwiftUI, you have to build the foundation you need to work with a note-taking App. It is made up of two classes: Note and NotesManager. The first one is used for the sake of illustration, a single note, and that the class brings to the table a total of three properties for the title, the content and the marking as a favorite. The properties can be set directly by using a suitable Starter, which is to provide for all three of the Properties have a default value. NotesManager it works like an App-wide Singleton for the Center for access to all of the notes. To do this, it is an Array in which to store all of your notes.

This is the first, basic versions of both classes can be found in figure 1. At a later date, the author of a couple of adjustments along the way.

// Listing 1: Deklaration der Klassen Note und NotesManager
class Note {
    var title: String
    var content: String
    var isFavorite: Bool
    
    init(title: String = "", content: String = "", isFavorite: Bool = false) {
        self.title = title
        self.content = content
        self.isFavorite = isFavorite
    }
}

class NotesManager {
    static let shared = NotesManager()
    var notes = [Note]()
}

For the Moment, there is not enough information to get started with the implementation of the User Interface of the note-taking App. As a Basis for this purpose, developers can use the The display of the content.swiftThe file is generated by Xcode automatically, along with the project. It contains a Dummy Code for the first View, and a Preview of the Supplier who has the Display to Display the developer tools.

The Application displays all of the information that is contained in your notes in list view mode. In the first step, you add a question to the The display of the content.swiftTo file for a View called NoteCelldirectly below ContentView it is being implemented. In View of this situation, the appearance of the cells in the list can be selected. For every note submitted, the developers were able to create in the next step, an instance of the View.

The View is not particularly complex. You may choose to show the title of the corresponding note, as well as a star chart, if a note is marked as a favorite. As a result, the NoteCell-See HStacka TextThe key to the title, and a Image the star chart contains. The latter is only shown if the note is one of your favorites. To be able to View it, properly set up, it requires a noteThe property that you leave a note with all the relevant information that is passed to it. Figure 2 shows the full implementation of the NoteCellStructure:

// Listing 2: Erstellen der Zelle für die Notizenliste
struct NoteCell: View {
    var note: Note
    
    var body: some View {
        HStack {
            Text(note.title)
            if note.isFavorite {
                Spacer()
                Image(systemName: "star.fill")
            }
        }
    }
}

The cell as the Basis for the developer to put it in the list view. To do this, review the ContentView[code]-Struktur innerhalb der ContentView.swift-Datei. Sie erhält zuerst eine Property namens [code]notesManagerthe Singleton NotesManagerThe class that is assigned to it. It serves as the source for the gantt chart View, you can view all the necessary information (for example, the notes that you want to display).

On the list of things you can do to implement it in the view with the name of a SwiftUI-View – List for you to use. You can expect a Range of a Project, the information that is contained in its cells. As a Range, using the notesThe array is created notesManagerThe property. This ensures that, for each observation in the matrix of the appropriate cell in the list that is generated.

At this point, the first adjustment to the Model, but also, and more specifically, to the NoteClass, in order to do so. List you can go by means of instances of the kind, in the Form of an array, if the type is in accordance with the IdentifiableFor the protocol to work. It’s only requirement is that the implementation of the idThe property, which is hashable. In this example, the developer has to generate, for each new observation is simply an instance on the Basis of the UUID and do you have the new one idProperty. The magazine NoteFirst-class you see in Listing 3:

// Listing 3: Ergänzung der Note-Klasse um das Identifiable-Protokoll
class Note: Identifiable {
    var id = UUID()
    var title: String
    var content: String
    var isFavorite: Bool
    
    init(title: String = "", content: String = "", isFavorite: Bool = false) {
        self.title = title
        self.content = content
        self.isFavorite = isFavorite
    }
}

Now List able, an Array-of – NoteGo through the forums. Listing 4 shows the corresponding implementation of the List the inside of the bodyThe property of the ContentViewThe structure is located here, by default, is contained in the Text, “Hello world.”). For each note, of a suitable instance of it is NoteCell generated as a result.

// Listing 4: Erstellen einer Listenansicht für Notizen
struct ContentView: View {
    var notesManager = NotesManager.shared
    
    var body: some View {
        List(notesManager.notes) { note in
            NoteCell(note: note)
        }
    }
}