let 聲明常量,var 聲明變量:
var myVar = 42
let myConstant =30
明確聲明類型:
var var1:Int;
拼接字符串:
let apples = 3; let fruit = "i have \(apples) apples"
統(tǒng)一[] 創(chuàng)建數(shù)組和字典;
var array = ["heloo", "water"]
array[1] = "friuit"
var dict = ["key1":"value1",
"key2":"value2"]
dict["key1"] = "vlue3"
創(chuàng)建空數(shù)組和字典:
let empArray = [String]()
let empDict = [String:Float]()
類型后面加一個(gè)問號來標(biāo)記這個(gè)變量的值是可選的。
var optionStr:String? = "hello"
switch支持任意類型的數(shù)據(jù)以及各種比較操作——不僅僅是整數(shù)以及測試相等。
if ,for ,switch 后面跟{}
switch vagetabl
{
case "calle":
print("call")
case "hello","hello2":
println("ok sog")
case let x where x.hasSuffix("pepper"):
println("write songs")
default:
println("nothing")
}
switch 不需要break語句;
for-in來遍歷字典
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],]
var largest = 0
for (kind, numbers) in interestingNumbers
{
for number in numbers
{
if number > largest
{
largest = number
}
}
}
for i in 0...5
{
//使用..<創(chuàng)建的范圍不包含上界,如果想包含的話需要使用...
}
var i = 0
while i<20
{
}
函數(shù)聲明:
使用func來聲明一個(gè)函數(shù),使用名字和參數(shù)來調(diào)用函數(shù)。使用->來指定函數(shù)返回值。函數(shù)可以嵌套,可以當(dāng)做返回值返回,也可以做為參數(shù)傳入;
func returnFifteen() -> Int {
var y = 10
func add() {
y += 5
}
add()
return y
}
returnFifteen()
func makeIncrementer() -> (Int -> Int)
{
func addOne(number: Int) -> Int
{
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
調(diào)用 increment(7)
func hasAnyMatches(list: [Int], condition: Int -> Bool) -> Bool
{
for item in list
{
if condition(item)
{
return true
}
}
return false
}
func lessThanTen(number: Int) -> Bool
{
return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(numbers,condition: lessThanTen)
入?yún)⒙暶鳎?conditon:Int -> Bool
函數(shù)實(shí)際是特殊的閉包,可用{}來創(chuàng)建匿名閉包;
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})
類,使用class聲明;每個(gè)屬性都需要賦值——無論是通過聲明(就像numberOfSides)還是通過構(gòu)造器(就像name)。
class NamedShape
{
var numberOfSides: Int = 0
var name: String
init(name: String)
{
self.name = name
}
func simpleDescription() -> String
{
return "A shape with \(numberOfSides) sides."
}
}
class Square : NameShape
{
var sideLength:Double = 0.0
init(sideLength:Double,name:String)
{
self.sideLength = sideLength;
super.init(name: name)
numberOfSlides = 4;
}
var perimeter:Double
{
get
{
return 3.0 * sideLength;
}
set
{
sideLength = newValue / 3.0;
}
willSet
{
triangle.sideLength = newValue.sideLength
}
didSet
{
triangle.sideLength = newValue.sideLength
}
}
func area() -> Double
{
return sideLength * sideLength;
}
override func simpleDescription() -> NSString {
return "A square sides of length \(sideLength)."
}
}
類中方法調(diào)用和普通函數(shù)區(qū)別:
函數(shù)的參數(shù)名只在函數(shù)內(nèi)部使用,但是方法的參數(shù)名需要在調(diào)用的時(shí)候顯式說明(除了第一個(gè)參數(shù))。默認(rèn)情況下,方法的參數(shù)名和它在方法內(nèi)部的名字一樣,不過你也可以定義第二個(gè)名字,這個(gè)名字被用在方法內(nèi)部。
class Counter
{
var count: Int = 0
func incrementBy(amount: Int, numberOfTimes times: Int)
{
count += amount * times
}
}
var counter = Counter()counter.incrementBy(2, numberOfTimes: 7)
類,枚舉,結(jié)構(gòu)體都可以實(shí)現(xiàn)協(xié)議;
使用protocol
來聲明一個(gè)協(xié)議。
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
class SimpleClass: ExampleProtocol
{
var simpleDescription: String = "A very simple class."
var anotherProperty: Int = 69105
func adjust()
{
simpleDescription += " Now 100% adjusted."
}
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescriptionstruct
SimpleStructure: ExampleProtocol
{
var simpleDescription: String = "A simple structure"
mutating func adjust()
{
simpleDescription += " (adjusted)"
}
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription
注意聲明SimpleStructure時(shí)候mutating關(guān)鍵字用來標(biāo)記一個(gè)會修改結(jié)構(gòu)體的方法。SimpleClass的聲明不需要標(biāo)記任何方法因?yàn)轭愔械姆椒ń?jīng)常會修改類。
使用extension來為現(xiàn)有的類型添加功能,比如新的方法和參數(shù)
extension Int:ExamplePro
{
var simDes:String
{
return "the number \(self)"
}
mutating func adjust() {
self += 42
}
}
println(7.simDes)
在尖括號里寫一個(gè)名字來創(chuàng)建一個(gè)泛型函數(shù)或者類型
func repeatIem<Item>(item:Item,numberOfTime:Int) ->[Item]
{
var result = [Item]()
for _ in 0..<numberOfTime
{
result.append(item)
}
return result
}
repeatIem("kenow", numberOfTime:4 )
http://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/chapter1/02_a_swift_tour.html
http://www.cocoachina.com/bbs/read.php?tid-206201.html
http://www.cocoachina.com/bbs/read.php?tid-205308.html
http://www.cocoachina.com/bbs/read.php?tid-204512.html
參考 http://www.cocoachina.com/bbs/thread.php?fid=57