let 聲明常量,var 聲明變量:
var myVar = 42
let myConstant =30
明確聲明類型:
var var1:Int;
拼接字符串:
let apples = 3; let fruit = "i have \(apples) apples"
統一[] 創建數組和字典;
var array = ["heloo", "water"]
array[1] = "friuit"
var dict = ["key1":"value1",
"key2":"value2"]
dict["key1"] = "vlue3"
創建空數組和字典:
let empArray = [String]()
let empDict = [String:Float]()
類型后面加一個問號來標記這個變量的值是可選的。
var optionStr:String? = "hello"
switch支持任意類型的數據以及各種比較操作——不僅僅是整數以及測試相等。
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
{
//使用..<創建的范圍不包含上界,如果想包含的話需要使用...
}
var i = 0
while i<20
{
}
函數聲明:
使用func來聲明一個函數,使用名字和參數來調用函數。使用->來指定函數返回值。函數可以嵌套,可以當做返回值返回,也可以做為參數傳入;
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()
調用 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)
入參聲明: conditon:Int -> Bool
函數實際是特殊的閉包,可用{}來創建匿名閉包;
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})
類,使用class聲明;每個屬性都需要賦值——無論是通過聲明(就像numberOfSides)還是通過構造器(就像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)."
}
}
類中方法調用和普通函數區別:
函數的參數名只在函數內部使用,但是方法的參數名需要在調用的時候顯式說明(除了第一個參數)。默認情況下,方法的參數名和它在方法內部的名字一樣,不過你也可以定義第二個名字,這個名字被用在方法內部。
class Counter
{
var count: Int = 0
func incrementBy(amount: Int, numberOfTimes times: Int)
{
count += amount * times
}
}
var counter = Counter()counter.incrementBy(2, numberOfTimes: 7)
類,枚舉,結構體都可以實現協議;
使用protocol
來聲明一個協議。
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時候mutating關鍵字用來標記一個會修改結構體的方法。SimpleClass的聲明不需要標記任何方法因為類中的方法經常會修改類。
使用extension來為現有的類型添加功能,比如新的方法和參數
extension Int:ExamplePro
{
var simDes:String
{
return "the number \(self)"
}
mutating func adjust() {
self += 42
}
}
println(7.simDes)
在尖括號里寫一個名字來創建一個泛型函數或者類型
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