iOS8開發~Swift(一)入門

一、概論及Swift介紹

iOS7剛發佈多時候,蘋果引入了JavaScriptCore.framework用來處理JavaScript,看到了可以接觸其他編程語言的契機,使iOS程序員不用吊死在OC這一顆樹上。當但iOS8發佈的時候,簡直Hold不住了,新的開發語言《Swift》出現了!Swift是一種新的編程語言,基於C和OC,可用於Cocoa和Cocoa Touch編程。編寫代碼中充滿互動性和樂趣,且語法簡潔而傳神,可以使應用程序運行飛快。Swift是以後iOS和OS X的項目開發語言的選擇之一,或在您當前的應用程序中使用,因爲Swift與Objective-C可以並駕的使用,實際上Swift可以與C和OC混編。


Xcode6下載地址:http://pan.baidu.com/s/1D3Z0i


Swift的特點:

1、安全性:增強了類型安全與類型推斷,限制指針的直接訪問,並且自動管理內存,可以輕鬆地創建安全,穩定的軟件。

func configureLabels(labels: UILabel[]) {
    let labelTextColor = UIColor.greenColor()
    for label in labels {
        // label inferred to be UILabel
        label.textColor = labelTextColor
    }
}

其中:

func關鍵字是定義一個函數;

labels: UILabel[] 的意思是labels是形參名字,UILabel[]是形參類型,表明是一個數組,數組裏邊元素是UILabel類型都對象;

let labelTextColor = UIColor.greenColor() 是定義一個常量labelTextColor,以前的[UIColor greenColor] 變成了現在的UIColor.greenColor()實現方式

for label in labels { } 這個是一個for循環,OC的實現方式是這樣的  for(UILabel *label in (NSArray *)labels) {},但現在但方式更簡潔了
label.textColor = labelTextColor UILabel對象的textColor屬性設置,寫法上和OC沒區別


2、模塊化:包括自選,泛型,元組,以及其他現代語言特性。通過Objective-C的啓發並在此基礎上改進,Swift 代碼更容易讀和寫。

let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]
let sortedCities = sort(cities) { $0 < $1 }
if let indexOfLondon = find(sortedCities, "London") {
    println("London is city number \(indexOfLondon + 1) in the list")
}

其中:

let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]  這樣就定義了一個常量數組,其中元素上字符串類型,OC的字符串上@"string",但Swift字符串和C字符串寫法一樣,如 "string",OC快捷方式定義數組可以使用@ ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"],但現在Swift數組實現相對更簡潔了,和C數組寫法一樣。

if let indexOfLondon = find(sortedCities, "London") { }就是查找一個數組中是否存在某元素了,如果返回但Index存在,就打印

println()與NSLog但作用一樣,只不過NSLog(@"%d")換成了了現在println("London is city number \(indexOfLondon + 1) in the list"),用 \(indexOfLondon + 1) 這種形式替換了@"%d"這種格式化輸出。


3、功能強大:利用強大的模式匹配可以編寫簡單且表達性好的代碼。格式化字符串清晰自然。通過Swift可以直接使用Foundation和UIKit框架。

let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]
let sortedCities = sort(cities) { $0 < $1 }
if let indexOfLondon = find(sortedCities, "London") {
    println("London is city number \(indexOfLondon + 1) in the list")
}


4、代碼互動:使用playgrounds嘗試新的​​技術,分析問題,和原型用戶界面。(Playgrounds make writing Swift code incredibly simple and fun. Type a line of code and the result appears immediately.


5、更快

以上參考  https://developer.apple.com/swift/



二、編寫HelloWorld

一般學任何一門語言,第一個項目都會寫個HelloWorld,那這裏也按照規矩來一個:需要下載Xcode6,可以去官網下載https://developer.apple.com/cn/,稍後我會上傳到網盤。

下載並安裝完Xcode6之後,打開Xcode,並且新建一個項目(選擇Single View Application模版):注意選擇Language爲Swift



打開項目後,目錄結構如圖:



看到這裏,我們並不陌生,和OC項目結構一樣,Main.storyboard還在,只不過以前到 .h和.m文件沒有了,替換成了選擇到 .wift文件,但main.m文件沒有了,那以前OC項目中UIApplicationMain哪去了 ?彆着急,打開AppDelegate.swift



願開在這裏,雖然寫法變了,不過意並沒有改變,對號OC代碼,不難理解AppDelegate但定義形式,如果寫過JAVA和JS代碼,看到這裏一定興奮了,太熟悉了,但僅僅瞭解OC也沒必要擔心,因爲很容易理解。class定義一個類,其中有成員變量和方法,成員變量用var定義,方法用func定義。遮掩一個.swift文件替代了OC的 .h和.m文件,看起來很簡潔,也很容易理解。

項目大體結構瞭解了,那現在向世界問好吧:

打開ViewController.swift

import UIKit
class ViewController: UIViewController {                    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

OC代碼中的#import <UIKit/UIKit.h>現在被更簡潔的import UIKit替代,override func viewDidLoad() 重寫父類方法。在viewDidLoad()方法中添加如下代碼:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("HelloSwift")
    }

然後編譯和運行,控制檯打印HelloSwift,發現了沒有,連一句代碼結束分號都可以不用寫了。但這個iOS開發離不開UI,所以這個HelloSwift太簡單了,添加一個UILabel到頁面,然後展示HelloSwift吧:

代碼如下:

import UIKit

class ViewController: UIViewController {
                            
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let label = UILabel()
        label.textColor = UIColor.blackColor();
        label.backgroundColor = UIColor.redColor();
        label.text = "HelloSwift";
        label.frame = CGRect(x:0, y:100, width:320, height:44)
        
        self.view.addSubview(label)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

運行效果:



到這裏,學習Swift到第一個程序算結束了!更多內容請點擊這裏

歡迎加入羣共同學習和進步:QQ羣:170549973

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章