iT邦幫忙

2021 iThome 鐵人賽

DAY 3
0

前言

如同第一天所說,基本語法的練習實在是太無聊了。不如就馬上來實作,從做中學吧₍₍ ◝(●˙꒳˙●)◜ ₎₎

學習資源

To-do list應該就是任何應用程式上手的起手式ㄌㄅ!
因此這篇參考的資源如下

YouTube: Build To Do List App in Swift 5 (Xcode 12) - 2021 Beginners
Medium: Swift — 做個 To-Do List 吧( 基礎篇 )

設定專案

Cocoapods

透過 CocoaPods,開發者可以輕鬆地管理 Swift 或 Objective-C 的程式專案。

若果沒有相依性管理工具協助下,使用類庫便需要逐一下載並加入Xcode項目中,但當專案規模增大,要處理的類庫數目增加,逐一手動新增或更新絕對是費時失事之舉。開發者透過相依性管理工具(如 CocoaPods)幫助輕鬆管理Xcode項目,只需要透過簡單的指令就能管理類庫項目,節省大量配置和部署的時間。

就我的理解,像是node.js環境當中的npm,可以用來引入資料庫、並加以管理。

首先開啟一個新專案,此時在專案路徑底下打開終端機並初始化cocoapods。

pod init
open Podfile

打上上圖的指令pod 'RealmSwift',在這邊我們使用Realm這個套件

pod install

下載完之後,就可以來使用workspace。這個檔案包含了剛剛建立的Xcode專案、以及透過cocoapods下載的相依性類庫和文檔。

至於怎麼在mac裏面安裝cocoapods,我有點忘記我是怎麼做的,本來就有內建ruby,我好像是用brew :arrow_right: 什麼都用brew的me

workspace vs project

然後我就在想,所以workspace跟project是有什麼差別ㄇ? 查到了以下3個的名詞解釋: Targets, projects, workspaces

Targets

Targets specify in detail how a product/binary (i.e., an application or library) is built. They include build settings, such as compiler and linker flags, and they define which files (source code and resources) actually belong to a product. When you build/run, you always select one specific target.
It is likely that you have a few targets that share code and resources. These different targets can be slightly different versions of an app (iPad/iPhone, different brandings,…) or test cases that naturally need to access the same source files as the app.

Projects

All these related targets can be grouped in a project. While the project contains the files from all its targets, each target picks its own subset of relevant files. The same goes for build settings: You can define default project-wide settings in the project, but if one of your targets needs different settings, you can always override them there.
In Xcode, you always open projects (or workspaces, but not targets), and all the targets it contains can be built/run, but there’s no way/definition of building a project, so every project needs at least one target in order to be more than just a collection of files and settings.

Workspaces

If, however, your library (the subproject) is used by a variety of other projects (or their targets, to be precise), it makes sense to put it on the same hierarchy level – that’s what workspaces are for. Workspaces contain and manage projects, and all the projects it includes directly (i.e., not their subprojects) are on the same level and their targets can depend on each other (projects’ targets can depend on subprojects’ targets, but not vice versa).

也就是說,專案(project)間的相依性使得需要一個更高階層的專案形式來管理,即workspace。

在這個workspace裡面,除了本身的專案,現在還多了pod這個專案,其中包含podfile引入的frameworks。

MVC

在to-do這個專案裡面,接下來會試著跟著mvc的架構,如下,進行實作

Model: a wrapper of data
View: a representation of a user interface (UI)
Controller: an intermediary between the Model and the View

UIViewController

UIViewController主要負責

  • Updating the contents of the views, usually in response to changes to the underlying data.
  • Responding to user interactions with views.
  • Resizing views and managing the layout of the overall interface.
  • Coordinating with other objects—including other view controllers—in your app.

每個app至少會有一個這樣的controller去處理畫面所要呈現的邏輯,而我們接下來的畫面邏輯會寫在這邊。

首先,把主要功能列出來,包含

  • 可以顯示清單中的選項
  • 可以新增選項
  • 可以刪除選項
  • 保留新增完的選項(重新打開app不會不見)

資料結構

在這個清單裡面,試想單個物件內容應該長怎樣? 除了todo名稱之外,還加入了其他細節、如日期,然後包成一個物件。

class TodoListItem: Object {
    @objc dynamic var item: String = ""
    @objc dynamic var date: Date = Date()
}

@objc dynamic前綴使得接下來的東西可以在runtime時使用objctive-c dispatch。而之所以需要使用是因為我們會將物件資料儲存在Realm,根據官方文件我們將屬性加入前綴。

viewDidLoad

controller執行,將view load進記憶體後,所會執行的內容,通常我們會override掉初始值(所謂nib files)。

常見的幾個方法順序分別為:

結語

到這邊可以先complie(cmd+B),也沒有新增什麼內容XD 基本上就是原本的初始專案,應該要編譯成功!

初始畫面,會以tableView的方式呈現,一項一項的清單。
下一篇會繼續實作!

若上述內容有誤或可以改進的部分,歡迎留言以及提出任何指教~
謝謝 ୧༼◕ ᴥ ◕༽୨

參考資源

  1. CocoaPods 簡介 : 如何輕鬆管理 Swift / Objective-C 的類庫
  2. Xcode Project vs. Xcode Workspace - Differences
  3. Understanding Model-View-Controller (MVC) on iOS
  4. UIViewController

上一篇
Day#02 Swift 101
下一篇
Day#04 TableView
系列文
來寫看看app好了! Swift探索之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言