探索Swift的計畫分類學習紀錄索引Trista's APP quest·Follow10 min read·Jan 29, 2021--Share[基礎語法及應用]常數vs變數/型別宣告常數或變數使用前必須給定初值,常數或變數宣告為確定的型別後就無法變更,宣告常數或變數可加上型別標注(type annotation),說明要儲存的值的型別,宣告常數或變數時給值,可觸發型別推斷(type inference)trista-92958.medium.com型別檢查vs型別轉換型別檢查運算子 is :檢查一個實體是否屬於一個類別,也可檢查一個類別是否遵循了某個協定(protocol) 型別轉換運算子 as:將一個值向下型別轉換至子類別的型別,as?檢查向下型別轉換是否成功,as!強制執行向下型別轉換trista-92958.medium.com可選型別(optional type)optional type ? /強制解析(forced unwrapping)! /隱式解析可選型別或自動取值(implicitly unwrapped optional)! /optional 綁定(optional binding)…trista-92958.medium.com類別vs結構定義一個新的類別或結構時,實際上是定義一個新的型別。必須依照類別或結構這個規格來建立一個類別或結構的實體(instance)。類別是參考型別(reference type),結構是值型別(value…trista-92958.medium.com函式(function)傳入參數為常數,內外部參數,預設值參數,可變數量參數,輸入輸出參數,也是一種型別,巢狀函式trista-92958.medium.com閉包(closure)一種沒有名稱的函式,可根據上下文推斷型別,單行閉包可以隱藏return,提供參數名稱縮寫,尾隨閉包,可以捕獲值,是參考型別,非/逃逸閉包,自動閉包trista-92958.medium.com擴展(extension)對內建的型別新增計算實體屬性與計算型別屬性, 為已有的型別新增實體方法與型別方法,為類別新增便利建構器,已有的型別新增下標,為已有的列舉、結構和類別新增巢狀型別,讓一個已存在的型別遵循一個或多個協定trista-92958.medium.com委任(delegate)委任的對象設置為ViewController 本身,或新增繼承自UIViewController的子類別,或設置為ViewController 本身,對 ViewController 新增一個擴展,為 ViewController…trista-92958.medium.com協定(protocol)定義出為了完成某項任務或功能所需的方法、屬性,協定本身不會實作這些任務跟功能,協定可被當做一種型別trista-92958.medium.com[設計模式應用]Model-View-Controller(MVC) 架構 (Architectural)設計模式MVC之間的互動關係trista-92958.medium.comModel-View-ViewModel(MVVM) 架構 (Architectural)設計模式MVVM之間的互動關係trista-92958.medium.com[基礎UIKit及應用]UIKit 框架AppDelegate.swift負責應用程式的生命週期。有多個畫面就需要各自建立一個不同的…trista-92958.medium.comUITextFieldUITextField輸入內容的鍵盤上會有一個完成的按鈕,把按下完成按鈕會做什麼事設計成委任模式,交給委任的對象 — self :ViewController 本身,為 ViewController…trista-92958.medium.coUITextView可為不同輸入框元件設置不同的選取內容後的選單,建立選單的程式放到元件取得焦點時的方法,改按輸入框之外其他空白處來隱藏鍵盤trista-92958.medium.comUIButton使用 UIButton(type: UIButton.ButtonType)建立預設樣式的按鈕,使用 UIButton(frame: CGRect(x: 0, y: 0,width: 150, height: 30))建立自定義的按鈕trista-92958.medium.comUIDatePicker用來選取日期或時間,常用的屬性:preferredDatePickerStyle、 datePickerMode、minuteInterval、date、minimumDate、maximumDate、locale,NSDate 及…trista-92958.medium.comUIPickerView可以設定自定義的選項及數目, UIPickerView 的委任對象設置為新增繼承自UIViewController的子類別,或self:ViewController的類別,UIPickerView 與 UITextField 結合應用trista-92958.medium.comUIAlertController使用 UIAlertController的 addTextField {(textField: UITextField!) -> Void in textField.placeholder = “帳號”}函式,建立有輸入框的提示框。trista-92958.medium.comUIImageViewUIImageView 的 highlighted 狀態一般情況下,不會變成 highlighted。當 UITableView cell 點選變成 highlighted 時,cell 上的UIImageView 會自動變成…trista-92958.medium.comUITableView使用 myTableView.register(UITableViewCell.self, forCellReuseIdentifier: “Cell”) 註冊 reuse 的 cell ,使用 UITableView 屬性…trista-92958.medium.comUICollectionView需先建立 UICollectionViewFlowLayout,用來自定義呈現的樣式 ,建立 UICollectionView 元件,要註冊 cell 及 header 或 footer,cell 需要自定義一個繼承自…trista-92958.medium.comUISearchController使用 UITableView 配合 UISearchController ,做一個搜尋框功能。委任的對象設置為self :ViewController 本身,對 ViewController 新增一個擴展,在新增的擴展檔案為…trista-92958.medium.comUIScrollViewUIScrollView可以縮放的元件需要設置委任對象self:ViewController來實作委任方法。UIScrollView配合 UIPageControl 建立左右滑動的分頁功能。iOS 14 版本以上UIPageControl…trista-92958.medium.com多頁面切換多個畫面就需要各自建立一個不同的 UIViewController,按下按鈕執行切換頁面的方法,按下按鈕執行退出頁面回前頁的方法trista-92958.medium.comUINavigationController是一個容器,需要設置一個 rootViewController,設置Navigation Bar上 button的方法,UINavigationController執行切換頁面及退出頁面回前頁的方法trista-92958.medium.comUITabBarController使用 UITabBarController 的 tabBar 屬性,設置TabBar的樣式- 使用系統內建的圖示 — 設置自定義圖示:有預設圖片及按下時圖片trista-92958.medium.comUIGestureRecognizer可以為元件加上六種不同的手勢監聽:Tap 輕點、 Long Press 長按、 Swipe 滑動、 Pan 拖曳、 Pinch 縮放及Rotation…trista-92958.medium.comAnimationsUIView.animate(withDuration:, delay:, usingSpringWithDamping:, initialSpringVelocity:, options:, animations:{}…trista-92958.medium.comNSUserDefaultsfunc set(_ value: Any?, forKey defaultName: String), func synchronize(),func object(forKey defaultName: String) ->…trista-92958.medium.com[iOS常用及特殊功能應用]加入 SQLite 及新增、修改、刪除與讀取資料使用sqlite3_open()函式開啟資料庫連線,使用sqlite3_exec()函式建立資料表,使用sqlite3_prepare_v2()和sqlite3_step()函式新增/修改/刪除資料,使用sqlite3_prepare_v2…trista-92958.medium.comSQLite 功能獨立在類別中SQLite功能獨立寫成一個類別。為類別建立一個可失敗建構器,為了確保有正確連結上資料庫。使用SQLite獨立類別來操作資料庫。trista-92958.medium.com加入 Core Data 及新增、修改、刪除與讀取資料NSEntityDescription.insertNewObject()新增資料,NSFetchRequest<NSFetchRequestResult>()建立一個取得資料的請求,宣吿操作 Core Data 的常數的…trista-92958.medium.comCore Data 功能獨立在類別中Core Data 功能獨立寫成一個類別,根據 attribute 的類型 : Entity類別名稱.entity.attributesByName[key]?.attributeType來轉換型別,以字串傳入新增、修改的Core…trista-92958.medium.com播放音效使用 AVAudioPlayer(contentsOf: NSURL.fileURL(withPath: soundPath!))函式初始化播放器,播放器的屬性 numberOfLoops 設定重複播放次數trista-92958.medium.com[串接 API 功能應用]串接 Airtable API 查詢及新增資料訂飲料APP 實做應用trista-92958.medium.com串接 Airtable API 修改資料訂飲料APP 實做應用trista-92958.medium.com串接 Airtable API 刪除資料訂飲料APP 實做應用trista-92958.medium.com