Swift開發圖解入門

來源:互聯網
上載者:User

標籤:求和   code   raw   分支   產生   tps   pop   and   顯示   

《論語·衛靈公》有一段經典對白:『子貢問為仁。子曰:工欲善其事,必先利其器。

……』。

對於一個程式猿來說,好的工具不意味著一定能產生優質的代碼。可是好的工具對提升開發效率的作用還是不言而喻的。想要用Swift做iOS開發。唯一可選的利器就是Xcode6了,童鞋們可以從以下的網站獲得Xcode6的下載連結,在此頁面中還可以找到Swift的官方書籍、例子代碼和視頻。

Xcode6下載:https://developer.apple.com/swift/resources/

接下來我們能夠開始建立一個項目Swift01,所看到的:








以上看到的就是入門程式『Hello, World』。

接下來要做的就是能夠通過構建整個項目或者直接通過執行項目並在控制台中查看結果,例如以所看到的:


好了,一切就這麼簡單。可是我們要講的肯定不是Hello World那麼簡單,通過以下的代碼先對Swift有個感性的認識吧。

import Foundationvar a:Int = 123   // 定義Int類型的變數alet b = 321       // 定義常量b通過類型判斷為Intprintln("\(a) + \(b) = \(a + b)")   // 字串中的\()能夠放變數、常量、運算式                                    // 輸出:123 + 321 = 444// 從1加到100求和var sum = 0for var i = 1; i <= 100; ++i {      // for迴圈    sum += i}println("SUM = \(sum)")             // 輸出:SUM = 5050for ch in "hello" {                 // 對hello的每一個字元進行迴圈    println(ch)}var randomAge = arc4random() % 50   // 調用Foundation中的函數產生隨機數print("You're \(randomAge) years-old! ")var myAge:UInt32 = 33;if randomAge > myAge {              // if-else分支結構     println("Older than me!")}else {    println("Younger than me!")}// 依據如今的系統時間顯示提示// [吐槽]時間日期的操作在蘋果的程式設計語言裡面真麻煩var currDate = NSDate()                                         // 建立時間日期對象var myCal = NSCalendar(calendarIdentifier:NSGregorianCalendar)  // 建立日曆對象(陽曆[格里高利曆])// 以下兩行代碼取出目前時間日期的小時部分var myComp:NSDateComponents = myCal.components(NSCalendarUnit.HourCalendarUnit, fromDate:currDate)var hour = myComp.hourswitch hour {case 7, 8:    println("Enjoy your breakfast!")case 11, 12, 13:    println("Have a good lunch!")case 18, 19:    println("It's a good time for dinner!")default:    println("Do something to make yourself happy.")}var myArray:Int[] = [12, 35, 96, 47, 5, 23, 68]myArray += [32, 81]println(myArray)    // 輸出: [12, 35, 96, 47, 5, 23, 68, 32, 81]// 定義找出數組中最大和最小元素的函數// 聲明函數的keyword是func, findMaxAndMin是函數名// 函數名後的()中是參數列表, 其參數是Int型的數組,// 傳回值是一個元組(包括多個資料的複合值), 包括兩個整數// 函數的傳回值是在函數參數列表後的->之後指定其類型的func findMaxAndMin(x:Int[]) -> (Int, Int) {    var min:Int = x[0]    var max:Int = x[0]    for var index = 1; index < x.count; ++index {        if x[index] > max { max = x[index] }        else if x[index] < min { min = x[index] }    }    return (min, max)}println(findMaxAndMin(myArray))     //輸出: (5, 96)// 定義一個字典類型(索引值對映射)var myDic = [1001:"Apple", 1002:"Banana", 1003:"Grape"]for (no, name) in myDic {    println("key = \(no), value = \(name)")}myDic[1003] = "Strawberry"println(myDic)         // 輸出: [1001: Apple, 1002: Banana, 1003: Strawberry]var myFruit1 = myDic[1002]  // Bananavar myFruit2 = myDic[2001]  // nilif myFruit2 == nil {    println(myFruit1)}else {    println(myFruit2)}// 將函數作為函數的參數(Lambda函數)func repeat(count:Int, callback: () -> ()) {    for i in 0..count {        callback();    }}// 調用repeat函數輸出10次Hello, world!repeat(10, { println("Hello, world!") })

Swift開發圖解入門

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.