蘋果的新程式語言 Swift 簡介
關於
這篇文章簡要介紹了蘋果於WWDC 2014釋出的程式語言——Swift。
前言
接下來進入正題。
Swift是什麼?
Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible and more fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to imagine how software development works. Swift is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.
簡單的說:
- Swift用來寫iOS和OS X程式。(估計也不會支援其它屌絲系統)
- Swift吸取了C和Objective-C的優點,且更加強大易用。
- Swift可以使用現有的Cocoa和Cocoa Touch框架。
- Swift兼具編譯語言的高效能(Performance)和指令碼語言的互動性(Interactive)。
Swift語言概覽
基本概念
Hello, world
類似於指令碼語言,下面的程式碼即是一個完整的Swift程式。
C1 | println("Hello, world") |
變數與常量
Swift使用var
宣告變數,let
宣告常量
123 | varmyVariable=42myVariable=50let myConstant=42 |
型別推導
Swift支援型別推導(Type Inference),所以上面的程式碼不需指定型別,如果需要指定型別:
1 | let explicitDouble:Double=70 |
Swift不支援隱式型別轉換(Implicitly casting),所以下面的程式碼需要顯式型別轉換(Explicitly casting):
C123 | let label="The width is "let width=94let width=label+String(width) |
字串格式化
Swift使用\(item)
????????????
1234 | let apples=3let oranges=5let appleSummary="I have \(apples) apples."let appleSummary="I have \(apples+oranges) pieces of fruit." |
?????
Swift??[]
????????array?????dictionary??
12345678 | varshoppingList=["catfish","water","tulips","blue paint"]shoppingList[1]="bottle of water"?varoccupations=[????"Malcolm":"Captain",????"Kaylee":"Mechanic",]occupations["Jayne"]="Public Relations" |
?????????initializer?????????????
C12 | let emptyArray=String[]()let emptyDictionary=Dictionary<String,Float>() |
??????????????[]
????????[:]
??????
???
??
Swift???????if
?switch
???????for-in
?for
?while
?do-while
???/?????????????/????body??????
123456789 | let individualScores=[75,43,103,87,12]varteamScore=0forscoreinindividualScores{????ifscore>50{????????teamScore+=3????}else{????????teamScore+=1????}} |
????
??if
?let
?????????????nullable variable??????????????????
??????????
12345678 | varoptionalString:String?="Hello"optionalString==nilvaroptionalName:String?="John Appleseed"vargretting="Hello!"iflet name=optionalName{gretting="Hello, \(name)"} |
靈活的switch
Swift中的switch
支援各種各樣的比較操作:
1234567891011 | let vegetable="red pepper"switchvegetable{case"celery":????let vegetableComment="Add some raisins and make ants on a log."case"cucumber","watercress":????let vegetableComment="That would make a good tea sandwich."caseletxwherex.hasSuffix("pepper"):????let vegetableComment="Is it a spicy \(x)?"default:????let vegetableComment="Everything tastes good in soup."} |
?
????
for-in
????????????????
1234567891011121314 | let interestingNumbers=[????"Prime":[2,3,5,7,11,13],????"Fibonacci":[1,1,2,3,5,8],????"Square":[1,4,9,16,25],]varlargest=0for(kind,numbers)ininterestingNumbers{????fornumberinnumbers{????????ifnumber>largest{????????????largest=number????????}????}}largest |
while
???do-while
???
1234567891011 | varn=2whilen<100{????n=n*2}n?varm=2do{????m=m*2}whilem<100m |
Swift?????for
????????????..
?????????for-in
????????
1234567891011 | varfirstForLoop=0foriin0..3{????firstForLoop+=i}firstForLoop?varsecondForLoop=0forvari=0;i<3;++i{????secondForLoop+=1}secondForLoop |
???Swift??..
??...
?..
???????????...
??????????
?????
??
Swift??func
????????
1234 | func greet(name:String,day:String)->String{????return"Hello \(name), today is \(day)."}greet("Bob","Tuesday") |
?????Tuple???????
C1234 | func getGasPrices()->(Double,Double,Double){????return(3.59,3.69,3.79)}getGasPrices() |
????????????
C123456789 | func sumOf(numbers:Int...)->Int{????varsum=0????fornumberinnumbers{????????sum+=number????}????returnsum}sumOf()sumOf(42,597,12) |
??????????
C123456789 | func returnFifteen()->I |