Swift初窺----文法

來源:互聯網
上載者:User

標籤:swift   class   ios   

Swift知識點

下面來點實際寫Swift代碼要用到的,首先Swift是沒有標頭檔的,沒有.h檔案。

變數聲明

Swift的變數聲明有兩個關鍵字,var和let,其中var和javascript中的差不多,可以聲明任何類型的對象,還可以通過var mystring:String 來指定類型。 

var myVariable = 42myVariable = 50let myConstant = 42

函數

Swift的函數表達,與javascript的關鍵字是一樣的,都是func,然後接函數名,注意的是括弧內的分別是形參名name,類型String,形參名day,類型String,其中->String是函數返回參數的類型。

func greet(name: String, day: String) -> String {    return "Hello \(name), today is \(day)."}greet("Bob", "Tuesday")


類和對象

類的聲明,其中類的方法定義在類的大括弧內。

class Shape {    var numberOfSides: Int = 0    func description() -> String {        return "A shape with \(numberOfSides) sides."    }}


協議

protocol ExampleProtocol {    var simpleDescription: String { get }    mutating func adjust()}
相關文章

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.