Day#03 初始专案

前言

如同第一天所说,基本语法的练习实在是太无聊了。不如就马上来实作,从做中学吧₍₍ ◝(●˙꒳˙●)◜ ₎₎

学习资源

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

<<:  Day6一个网站总需要一点首页吧!

>>:  Day 06 Heroku、Heroku CLI、Git push建置

Day25:安全性和演算法-讯息监别码(Message Authentication Code)

讯息监别码(Message Authentication Code) 讯息监别码(Message A...

Day03-CRUD API 实作(三)Route、Postman 测试 API

大家好~ 昨天完成会员系统的 Controller, 今天要来完成会员系统的 Route , 并且用...

推论 & 聚合( Inference and Aggregation)

-聚合功能 分区(Partitioning) 对数据或数据库进行分区是指将数据集分成多个部分并分别...

前谈依赖注入(Dependency Injection)

直觉的物件导向程序 Car 类与 Engine 类的组合, Car 执行 run 前先产生 Engi...

【从实作学习ASP.NET Core】Day07 | 後台 | 复杂的商品模型

前面花了点时间介绍了 MVC,今天终於要进入正题啦! 我会以一个电玩专卖店的购物网站为主题,并且从後...