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

Sam Blog, Sam Sharing, Sam Studio

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月25日

[Mac] 好用的檔案加密工具 - 磁蝶工具程式


最近在找如何將檔案隱藏或加密的方法,發現Mac 的磁蝶工具程式提供了一個好用的檔案加密功能!

Step1: 啟動 "應用程式 -> 工具程式 -> 磁碟工具程式.app"


Step2: 選擇 "新增映像檔" 來製作空白磁碟映像檔(DMG檔案)


Step3: 輸入映像檔檔名(如Test.dmg), 檔案大小(如100 MB), 記得選擇 "加密保護",否則這映像檔就沒有加密保護了,這裡我選擇128 bit AES 加密保護,最後在按下"製作"來產生映像檔


Step4: 按下製作就可以在你指定得資料夾看到系統為你產生的磁碟映像檔,另外桌面也會有個外接的磁碟映像檔圖示產生; 到這裡你就完成了一個具有檔案加密功能的磁碟映像檔.



Step5: 接下來就告訴你如何使用它;首先先退出這外接的磁碟映像檔回復到未打開這加密功能的磁碟映像檔,然後再Finder打開"Test.dmg",這時Finder就會跳出輸入密碼的畫面, 也就是只有密碼的人才可以打開(這正是我們要的)

PS: 這裡有兩點注意事項
1. 千萬不要勾選 " 在我的鑰匙圈中記錄密碼",否則密碼一旦被系統記錄起來,跳出輸入密碼的畫面就不會再出現了,因為系統每次都會自動幫你填入,就失去加密的功能了
2. 當你輸入密碼打開這加密功能的磁碟映像檔後,下次你再要開啟這檔案, Finder是不會再要求你輸入密碼的除非你有做 "退出磁碟映像檔"這動作




Step6: 打開這磁碟映像檔後,你會可以將你要保護的帳密檔案拉到這映像檔來做密碼保護,記得拉完檔案後要做 "退出磁碟映像檔" 這動作ㄡ!

2014年9月22日

沒誠意的 Wacom 手寫板


最近買了Wacom 手寫板來做 Icon 的開發,也下載了Windows 的附贈軟體,很高興地使用中...
同時MacBook Pro 的筆電也在近日收到,想說去下載Mac OS 的版本來試試,結果Wacom不給下載, 無法更換成Mac OS版本, 我只能在我的舊筆電畫了, 真是一肚子怨氣...

2014年9月16日

[Mac] 更改檔案關聯程式


最近發覺 MAC 的 "預覽程式" 在檢視 PDF 的檔案時, 反應速度明顯比Reader 還快!
當時在安裝 Reader 時已經同意Reader 去更改PDF 的檔案關聯,如今要改回預覽程式要如何做?

Step1: 至Finder 選擇你要更改的檔案關聯,我以 "PDF" 為例;然後按右鍵,就有如下選單

Step2: 請選擇 "簡介" , 就會看到如下選單, 紅色框框顯示 PDF 的檢視程式為 Reader


Step3:  在 "下拉式選單" 選擇你要的檢視程式就完成檔案關聯的更改







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.

熱門文章