close

最近宅宅工程師,阿軒,也就是我,為了避免未來老年失業,開始自學了專門用於iOS系統的程式語言"Swift"。

網路上有許多教學文章可以參考,不過絕大部分都是英文的,還有少數簡體中文的。

不能期待所以繁體中文使用者都內建簡體閱讀能力,就讓我來幫大家整理一下吧。


對於初學者來說,像是宅宅我本人,相當適合從看史丹佛大學的教學影片來著手,雖然不知道講師叫什麼名字,但這位大叔講得著實不錯。

下面附上影片在youtube的超連結。


講師一開始先幫iOS簡略分為四層來做介紹,由最靠近硬體的底層開始到靠近用戶的頂層分別為Core OS、Core Services、Media、Cocoa Touch

Core OS包括以下內容:

OSX Kernel          Power Management

Mach 3.0              Keychain Access

BSD                     Certificates

Sockets                File System

Security                Bonjour


Core Services包括以下內容:

Collections           Core Location

Address Book      Net Services

Networking           Threading

File Access          Preferences

SQLite                  URL Utilities


Media包括以下內容:

Core Audio           JPEG, PNG, TIFF

OpenAL                 PDF

Audio Mixing         Quartz (2D)

Audio Recording   Core Animation

Video Playback     OpenGL ES


Cocoa Touch包括以下內容:

Multi Touch             Alerts

Core Motion           Web View

View Hierarchy       Map Kit

Localization            Image Picker

Controls                  Camera   


不過以上分類並不是這門課的重點,只是先科普一下而已。

接下來要進入我們的主要內容。


首先請大家先去iTune下載最新版本的Xcode,這是Apple釋出的開放性編輯程式,功能強大,相當好用。

我目前所使用的是最新版本,Version 7.1.1,打開後會看到以下畫面。



這時候請直接點選"Create a new Xcode project",下面將要開始寫一個簡單的範例。

我這邊就不說明StroyBoard的使用方式了,大家可以自己看影片揣摩一下,我這邊主要只整理Coding的部分,並做說明。

當你完成初步的storyboard編輯之後,點選左邊項目欄位的"ViewController.swift"就可以看到原始的程式碼。

在課堂的範例中,並不需使用到 class ViewController: UIViewController { } 裡面的內容,各位可以先全部刪除。

接下來講師將計算機介面的顯示螢幕與數字鍵導入編碼中,並加上一些敘述,使其可以初步工作。

(由於畫面是我學完之後自己重新做一遍後擷取的,所以有些命名會不太一樣,大家意會就可以了。)

@IBOutlet weak var display: UILabel! 與 @IBAction func numberkey(sender: UIButton) 是storyboard中的 Label 與 Botton 拉進來之後直接產生的。

在@IBAction func numberkey(sender: UIButton) { } 內加上 "display.text = sender.currentTitle" 的敘述是為了讓顯示螢幕可以得到我們所點擊的數字按鍵。


display 後面加上 ".text" 代表 display 的 String 型態。

參考文檔說明如下:

Declaration : 

var text: String? { get set }

Description : 

The text displayed by the label.

This string is nil by default.

In iOS 6 and later, assigning a new value to this property also replaces the value of the attributedText property with the same text, albeit without any inherent style attributes. Instead the label styles the new string using the shadowColor, textAlignment, and other style-related properties of the class.


sender.currentTitle 是發送的內容等同於數字按鍵 (numberkey) 的當下 Title 名稱。

參考文檔說明如下:

Declaration : 

var currentTitle: String? { get }

Description : 

The current title that is displayed on the button. (read-only)

 The value for this property is set automatically whenever the button state changes. For states that do not have a custom title string associated with them, this method returns the title that is currently displayed, which is typically the one associated with the UIControlStateNormal state. The value may be nil.


但此時顯示螢幕只能根據我們做點擊的數字鍵變換數字,而正常的計算機上的螢幕應該會不斷累加我們所輸入的數字,所以剛剛的程式碼必須做進一步的修改。

先預設一個布林變數 "displayvalue" 來表示顯示螢幕中是否已輸入數值。

一開始的顯示螢幕中並沒有經由任何案件輸入任何數值,所以雖然有的 "0" 在上面,但實際上我們當它是空白的,以 "nil" 表達。

這時我們將 "displayvalue" 預設為 false ,同樣代表 "nil" 。

此 "dispalayvalue" 並未與 display 在程式碼上有任何關聯,我們只是藉由相同的狀態模擬來在不同情況下得到我們所希望的動作。

接著利用 "if & else" 來做條件控制。


"if displayvalue { }" 的意思是當 "displayvalue" 為 true 時,進行括弧內的動作。

這裡我們寫的動作是 "display.text = display.text! + number" ,意思是將顯示螢幕上的值在原本的基礎上,將新輸入的數值疊放在其之後。

舉例來說,假設原本螢幕已經顯示 1 (display.text) ,當我們按下 2 (number) 的按鍵,display.text 的值就會更新為 "12" 。


這裡有幾個新內容要進行說明。

為了讓我們按下按鍵後,其值可以同步顯示在螢幕上,我們必須創造一個變數 (var) number 。

let number = sender.currentTitle!

"let" 是用來建立一個常數項的指令,因為我們的 number 永遠等同於按鍵的數值,之後不會改變,所以直接設為常數。

與他類似的另一個指令是 "var" ,功能與 let 相似,差別在於其是用於建立變數項。

上面的敘述中,我們將按鍵所送出的資料設定為 "sender.currentTitle" 。

"sender" 指的是當我們在介面中點擊按鍵時所送出的內容,".currentTitle" 則是指當下按鍵上的 Title 名稱,於是 "sender.currentTitle" 就是指送出與當下按鍵的 Title 名稱相同的數值。

".currentTitle" 參考文檔說明如下:

Declaration : 

var currentTitle: String? { get }

Description : 

The current title that is displayed on the button. (read-only)

The value for this property is set automatically whenever the button state changes. For states that do not have a custom title string associated with them, this method returns the title that is currently displayed, which is typically the one associated with the UIControlStateNormal state. The value may be nil.


另外一點要注意的是,在某些地方你會看到一個驚嘆號 "!",有時候會內建出現,但有時候我們必須自己主動加上。

而什麼時候需要加上呢?

當你的變數或者函數等參數之類型 (type) 不確定時,Swift 裡稱之為 "option",這也是一種類型,他可以藉由程式的自行判斷來扮演 "string" 和 "character" 等等類型。

但他依然是一種不確定的型態,當需要與其他形態做互動時,就可能會出現錯誤。

此時我們就必須藉由在後面掛一個驚嘆號 "!" 來解包 (unwrap) 。

這個動作可以將 "option" 這個型態轉換為程式根據上下文來判斷出的其他形態。


回到程式碼的部分。

如果我們只寫了 "display.text = display.text! + number" ,那麼大家就會發現,雖然已經可以在點擊按鍵後將結果累加在原本的數字後面,但是有個問題。

原本上面的 "0" 也會留在最前面了!!!

對一個計算機來說,你想要輸入 "123" ,結果上面顯示 "0123" ,很不自然呀....

於是我們還必須再提供另一個條件。

else {

display,text = number

displayvalue = true

}

接續前面 "if" 的條件,在 "else" 裡面,當 displayvalue 為 false 時。執行裡面的動作。

也就是說,城市剛開始執行時,也就是顯示螢幕上為 "0" 時,將顯示螢幕上的值更換為所點擊之按鍵的數值,於是就不會將 "0" 疊加在最前面了。

接著再將 displayvalue 的狀態改為 true ,那麼之後就會進行前面的疊加動作。


第一堂課其實就講到這裡而已。

但不知道是後面會修正還是講師沒有發現,這裡還有一個小 BUG 存在。

當你如果第一個數字輸入 "0" 時,後面還是會將 "0" 疊加在最前面,於是我私人又做了下面的改善。

承上啟下,這堂課既然只教了 "if" 的用法,那麼就一樣用 "if" 來 De-Bug 吧。

在 displayvalue 為 false 的條件中再另外加上一組 "if & else" 的條件句。

 if display.text! == "0" 

{displayvalue = false}

 else 

{displayvalue = true }

很簡單吧,在 displayvalue 為 false 時,先判斷剛剛所點擊的按鍵是否為 "0" ,若是的話就回傳 false 的結果,讓迴圈重來一遍。

若為否再將狀態回傳為 true ,接著進行數字疊加的動作。


這並不算是聰明的方法,相信後面會學到更好的工具來簡化這段程式碼。

這堂課到此結束,我們第二堂課再見~


By 宅宅阿軒

arrow
arrow
    文章標籤
    Swift iOS8
    全站熱搜

    Cloud 發表在 痞客邦 留言(0) 人氣()