山姆的編程實作分享。。。

Sam Blog, Sam Sharing, Sam Studio

顯示具有 iOS 標籤的文章。 顯示所有文章
顯示具有 iOS 標籤的文章。 顯示所有文章

2015年1月26日

2015年1月7日

Swift Programming - Fallthrough statement


Swift 的控制轉移敘述 (control transfer statements)共有 break, continue, fallthrough  及 return 四種.

break, continue & return 與Objective-C 同, Fallthrough 是 Swift 新加入的.

Fallthrough 語法定義如下:
Fallthrough 有以下幾點特性:

1. Fallthrough 只會(能)出現在Switch 敘述中的case block

2. Fallthrough 不會檢查 case condition 且直接執行下一個 case block (或default case block)

3. Fallthrough 不能出現在最後ㄧ個 case block

資料參考自: Apple, The Swift Programming Language

2015年1月4日

Swift Programming - Switch statement


Swift 的 Switch statement 和 Objective-C 有以下幾點差異

1. 語法定義:

從語法上可以看到,我們不必在每個case statement 後加上break, Swift 會自動結束switch case 區段.

2. 從上面語法定義可以發現, Switch 增加 "where" 關鍵字,它(where condition)提供額外的條件敘述來加強case, 使case 條件更加嚴謹.
例如:
case (let x,  let y) where ( x == y )

3. 每個 case block 必須至少一個 statement, 如果真的沒有要執行的程式碼,那就請加上"break"吧.

The scope of each case can not be empty. As a result, you must include at least one statement following the colon (:) of each case label. Use a single break statement if you don’t intend to execute any code in the body of a matched case

資料來源: Apple, The Swift Programming Language

2014年12月29日

Swift Programming - For-In loop statement (迴圈敘述)


Swift 同Objective-C提供了 for, while 及 do-while 迴圈指令 ( loop control construct ), 除此之外, Swift 新增了 for-in loop control; 針對 array, dictionary 及 string 的應用, for-in 有他好用之處.

以下是他的語法定義:


以下我們就用playground來試用 for-in control statement


以下為array 的應用


以下為string 的應用


資料參考來自:
Apple, The Swift Programming Language.

2014年12月16日

『Gartner』Sales of Smartphone in Q3 2014


前五大智慧型手機廠商銷售統計, Samsung 還是第一, 但市佔率已經從32.1%掉到24.4%, 小米已經排名第四了, 大陸廠牌就佔了3位.




















智慧型手機作業系統銷售統計, Android佔了83.1% 和 iOS就吃掉95%的市佔率, Windows只有3%.

















全球手機銷售前十大廠商, 在台灣還賣得不錯的HTC 及 Sony竟然排不上


























資料來源:Gartner

2014年12月4日

Swift Programming - Tuples type (多元組 / 元組)


除了一般我們熟悉的資料型態外, Swift 加入了兩項Objective-C沒有的資料型態
1. Optional (選項型態)
2. Tuples (中文翻譯為 "多元組" or  "元組")

在iOS Computer Language 的世界,Tuples或許是新成員, 但是在Computer Language 的世界早已經可以看到它的足跡, 如C#, Python.

以下是它的語法定義:

從語法的定義可以知道它的宣告方式是由零到多種資料型態用分號分開再由小括號包覆的資料型態.

那Tuples要用在哪?就如 "Swift Programming Language" 文中所說的
Tuples are particularly useful as the return values of functions; Tuples are useful for temporary groups of related values. They are not suited to creation of complex data structures.

也就說,當你想回傳的參數不是單一值又不想定義ㄧ個資料結構時就可以使用Tuples.

另外,你也可以為每一個element命名
You can also name the elements of a tuple type and use those names to refer to the values of the individual elements. An element name consists of an identifier followed immediately by a colon (:).

Tuples常被用在錯誤的處理;
我們可以設想這樣的情境,當我們在處理error code的時候,如果只是回傳數字,對於程式設計員是足夠的且很好處理,但是對於終端的維修人員是可讀性低且必須查表才知道機器錯誤的原因,這是很不好的ㄧ種的設計; 相反的, 如果我們可以同時回傳字串的話那就比較符合人性.


資料參考來自:
Apple Inc. The Swift Programming Language.

2014年12月1日

Swift Programming - Optional type (選項型態)


除了一般我們熟悉的資料型態外, Swift 加入了兩項Objective-C沒有的資料型態
1. Optional (選項型態)
2. Tuples

選項型態 (optional type)在 "The Swift Programming Language" 文章中是這樣形容它的,

Optionals handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there is not a value at all”. Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes. Optionals are safer and more more expressive than nil pointers in Objective-C.

所以選項型態 (optional type)可以說成:
某變數或常數
有值且等於某值(there is a value, and it equals x)
或
無資料(there is not a value at all)

從語法的定義可以知道它的宣告方式就是在型態名稱後加在?
至於加上!, 則是當確定有資料時,解開選項型態時使用


以下我們就利用Playground來體驗選項型態 (optional type)
從上圖可以發現,
1. 在列印時,因為possibleString 是選項型態,在字串前面會Optional關鍵字.
2. 如果你沒有給選項型態的變數或常數初值,Swift為自動設 "nil" .

下圖為!的使用範例

資料參考來自:
Apple, The Swift Programming Language.

2014年11月23日

Learning Cocos2D - Create Cocos2D API Document Locally (製作 Cocos2D API Document)


Cocos2D 提供的 API document 為線上模式, 一旦在沒有網路的環境下是無法做查詢的,想在自己電腦也安裝一份API文件檔要如何做?

從Cocos2D原始檔案可以發現Cocos2D是用Doxygen製作 API Document



所以我們依樣畫葫蘆,用Doxygen來製作Cocos2D API Document

Step1: Download Doxygen

Step2: 安裝 Doxygen (因為Doxygen不是AppStore 或已識別的開發者應用程式,所以必須至系統設定暫時打開安全設定)

Step3: 打開 Doxygen 做如下設定


Step4: 設定完成後, 按下 "Run" 來產生文件


Step5: 執行"Run"完後按下上圖中的 "Show HTML output" 就可看到我們的成果


Step6: 大功告成!

2014年10月25日

Swift Programming - semicolons ( ; ,分號 )


分號(;)

不像 Objective-C, Swift 不需要在每段程式碼 (statement) 後加上分號,
但是如果你要在一行裡寫兩個 statements, 那麼你還是必需用分號來分隔兩個statement
例如:







資料參考自: Apple Inc. 「The Swift Programming Language。」 iBooks.

Swift Programming - println and print


println 及 print 是全域函數, 兩者的的差異則是println會在列印後加入跳行
以下為兩者差異的實例:























資料參考自:
Apple Inc. "The Swift Programming Language"
Apple Inc. "Swift Standard Library Reference"  

Swift Programming - Constant / Variable (常數 / 變數)


Swift 使用 var 關鍵字來宣告變數, 使用 let 關鍵字來設定常數

以下是用playground 試用Swift語言的程式碼


Swift並不需要你明確的指定資料形態,complier 會依照你給他的數值來決定他的資料型態
例如:






當然,你也可以在變數或常數後加上分號來明確的指定其資料型態,
例如:







另外數值的指定並不隱含資料形態的轉換,否則complie time就出錯了

例如:







你必須明確指定
例如:





完整的測試碼


















資料摘錄自: Apple Inc. 「The Swift Programming Language。」 iBooks.

2014年10月23日

Swift Programming - Preprocessor / Header File (前置處理器 / 標頭檔)


以下是C++語言的Hello, World! 的範例程式

// 下面這行是標頭檔
#include <iostream.h>

// 下面是主程式
void main()
{
    count <<"Hello, World!\n";
}

我想上面這段程式, 這對學過C/C++語言的人應該熟悉不過了,
對於Swift語言, 又要如何撰寫來完成同樣的工作呢?

println("Hello, World!")

沒錯, 就這麼ㄧ行 !
不要懷疑,就ㄧ行code就可以完成C++同樣的工作.

在Swift語言,
1. 你不需要特別import 像 input, output 及 string處理函數的 library;所以不用像C++的Hello, World !程式去include <iostream.h>
2. 你不需要main 函數當作程式的開始位置
3. 你也不需要在每行程式碼的結尾加上分號

以下我們就來實作,在螢幕在顯示"Hello World"

Step1: 開啟Xcode,新增一個Project,然後選擇OS X Command Line Tool

Step2: 請選 "Swift" language, 其他隨你填

Step3: 最終Xcode會幫你產生如下的樣板 (template)

Step4: 請將 import Foundation 這行刪除或註解掉, 因為我們目前只用到簡單的Output函數.

Step5: 按下左上方執行按鈕後,就可在右下角Output視窗看到輸出結果

資料摘錄自: Apple Inc. 「The Swift Programming Language。」 iBooks.

2014年9月9日

Swift Programming - A Swift Tour in Playground


Swift Programming 有一種互動式的開發環境叫做Playground;
Playground 可以在還沒 Compile & RUN 的狀況下, 提供一個快速反應 coding 結果的編輯模式!

Step1: Open Xcode 6 then Select "File  -> New -> Playground"


Step2: Input file name of playground then press "Next", the playground file be created.


Step3: Xcode will create below template file for you, as you see, the right side shows the result of your code.



By the way, you can also create a playground file in your existing project, by following steps
StepA: In project navigator, press right button then select "New File"


StepB: Please select "Playground" as template


StepC: Input file name then press "Create", the playground file will be created in project navigator



2014年9月4日

Swift Programming – File difference between Objective-C and Swift


Continue Swift Programming- Hello Swift , we create a "Hello Objective-C" Application to compare the difference between Swift and Objective-C

Below is Objective-C Application file structure

Below is Swift Application file structure



The differences have
1. No header file (*.h)
2. the C file (*.m) has changed to Swift file (*.swift)


Swift Programming - Hello Swift


Step1: Install Xcode 6 beta from Apple Developer site
             Xcode 6 Beta version

Step2: Create new project with "Swift" language


   Select Single View Application 



   Select "Swift" language


Step3: Add a label - "Hello Swift" into storyboard


Step4: Build and Run App on Simulator


All steps are almost same as Objective-C development flow except step3, select "Swift" language.

2014年8月13日

Swift Programming – Resource



Apple Swift 官網 :
https://developer.apple.com/swift/

Apple Swift Blog :
https://developer.apple.com/swift/blog/

Xcode 6 beta version download :
https://developer.apple.com/swift/resources/

Swift presentation, Docs and Sample code :
https://developer.apple.com/swift/resources/

The Swift Programming Language 中文翻譯版本 :
http://tommy60703.gitbooks.io/swift-language-traditional-chinese/



2014年7月9日

Learning Cocos2D - Install Cocos2D and make first Cocos2D project


Step1:  Download Cocos2D installer from below link

              http://www.cocos2d-swift.org/get-started

Step2: After installed Cocos2D framework, the Cocos2D template will exist in Xcode.
Press File -> New -> Project.., you will see below figure


Step3: Like iOS first simplest application - Hello World; You can follow step4 - Step7 to create your first Cocos2D application without input any code, just using Cocos2D template.

Step4: Continues Step2, select cocos2d as template then press "Next"

Step5: Input Product/Project Name as you want, here i input CCHelloWorld.

Step6: Select project folder then press "Create", the Cocos2D Hello World project will be created as below


Step7: Press "RUN"

Step8: Below pictures are the Cocos2D Hello World screen shot



熱門文章