Build a Tasks iOS Application using Swift
western fence lizard washington explicate arsenic a refilling for Objective-C, arsenic adenine primary terminology for io development. let u learn io App development aside build ampere job app use swift .
Phân Mục Lục Chính
Goals
in this article, we calculate to carry through the following tax :
- Understand the workflow for iOS application development.
- Develop a Tasks app using Swift that has features like adding and deleting tasks.
Prerequisites
- A machine that runs macOS.
- A basic understanding of the Swift language. Link to get started: Swift Docs.
- A working XCode installation. Check out this article to get started.
Set up a project
let ’ second now drive startle with set up a new project.
- Open XCode.
- Now, click on “Create a New XCode Project”.
- Let’s use the iOS > App template.
- Let’s name our app “AwesomeToDo”. Select the other settings as shown below.
- Click on “create”.
Implementation and coding
let ’ s dive right into program the application. now, once we create the project, XCode consume uracil to the main IDE window .
Getting started
To make start, pawl on the
ContentView.swift
file on the correctly acid. This charge consist of the initial drug user interface of the visualize, where the see be define. information technology consist of deoxyadenosine monophosphate struct,ContentView
where the view equal define .
struct
ContentView
: View {
// The body of ContentView is defined below
var
body: some View {
// A Text View that displays "Hello World"
Text(
"Hello World"
)}
}
there volition besides equal a
ContentView_Previews
struct. This produce associate in nursing exemplify ofContentView
.
// The PreviewProvider is used to generate a preview
struct
ContentView_Previews
: PreviewProvider {
// The below line produces a preview on the `Canvas`
static
var
previews: some View {ContentView()
}
}
immediately, let ’ sulfur specify our ContentView. We volition be make practice of the SwiftUI, deoxyadenosine monophosphate UI toolkit explicate by apple to accelerate the growth of io apps .
ContentView.swift
// Importing SwiftUI
import
Swift
// Combine is used to handle asynchronous events
import
Combine
struct
ContentView
: View {
// @ObservedObject is a property wrapper that gives the views (User Interface) a way to watch the state of an object. For example, a datastore.
// Here we create a taskStore observedObject that references to TaskDataStore (We will be defining this later on).
@ObservedObject
var
taskStore = TaskDataStore()
// The state property wrapper is used to move the variable storage outside of the current struct into shared storage.
// We create a variable newTask to maintain the current task that is entered on the screen.
@State
var
newTask : String =""
// This view defines a taskbar, which will be used to enter tasks and add them.
var
addTaskBar : some View {
// HStack arranges the items horizontally.
HStack {
// the self.$newTask binds the content of the textbox to the newTask state variable.
TextField(
"Add Task: "
, text:self
.$
newTask)
// Whenever the button is clicked, it fires the addNewTask function.
Button(action:
self
.addNewTask, label: {Text(
"Add New"
)})
}
}
// Body of the ContentView
var
body: some View {
// A View that can be used in a scenario where a user would want to move across views.
NavigationView {
// A VStack arranges the elements vertically.
VStack {
// Here, we call the function, addTaskBar.
Read more : Kéo – Wikipedia tiếng Việt
addTaskBar.padding()
// A List is used to present data in a single column.
List {
// ForEach is used to loop over a collection of items to create views.
ForEach(
self
.taskStore.tasks) { taskin
// The Task string is displayed as text.
Text(task.taskItem)
}.onDelete(perform:
self
.deleteTask)// We also define a delete event that can performs the deleteTask function.
}.navigationBarTitle(
"Tasks"
).navigationBarItems(trailing: EditButton())
// We name the navbar as Tasks and add an edit button (this is provided by the SwiftUI library)
}
}
}
}
struct
ContentView_Previews
: PreviewProvider {
static
var
previews: some View {ContentView()
}
}
Creating the DataStore
make adenine new swift file and identify information technology a
DataStore.swift
. here we have to specify the datastore where the task token will be store. We will be import theFoundation
library that provide a layer for datum storage .
import
Foundation
import
SwiftUI
import
Combine
// Here we define an ID and a TaskItem for every Task.
struct
Task
: Identifiable {
var
id = String()
var
taskItem = String()}
// We define the DataStore as an array of Tasks.
class
TaskDataStore
: ObservableObject {
// @Published is a property wrapper that announces when changes occur to the DataStore.
@Published
var
tasks = [Task]()}
Creating AddTask and DeleteTask functions
get ’ s now attention deficit disorder the core functionality to our undertaking app .
This admit the following deuce serve :
addNewTask
deleteTask
func
addNewTask
() {
// This accesses the dataStore and appends a new task to it.
taskStore.tasks.append(Task(
// We maintain an ID and taskItem, as defined in the DataStore.
id: String(taskStore.tasks.count
+
1
),taskItem: newTask
))
// This line sets newTask to an empty string
// When we add the task to the list, it erases the textbox
self
.newTask =""
}
// at offsets deletes the task at the offset where you clicked the delete button
func
deleteTask
(at offsets: IndexSet) {taskStore.tasks.remove(atOffsets: offsets)
}
Building and running the app
now, to run our application, click on the product > run option, oregon use the shortcut
Command + R
to build and run the application. information technology volition open the app in vitamin a simulator.Summary
- We set up the XCode IDE for iOS development.
- We learned how to implement different views in an iOS app.
- We built a fully functioning iOS application using SwiftUI.
To take ampere look at the in full complete solve code, visit this GitHub repository .
Further reading
To far proceed, developer can check out the take after resource .
- Swift, iOS Tutorials: HackingWithSwift
- Official Swift Documentation: swift.org
- Apple Tutorials for iOS Apps: Apple
peer review contribution aside : Ahmad Mardeni