此教程主要是依据为苹果官方文档,部分代码使用Python,Java语法注解
import Foundation
//用for in字典的迭代
let interestingNumber = [
"Prime":[2,3,5,7,11,14],
"Fibonacci":[1,1,2,3,5,8],
"Square":[1,4,9,16,25],
]
var largest = 0
for (kind,numbers) in interestingNumber{
// println(kind,numbers)
// println("kind:" + kind)
// println("numbers:", numbers)
for number in numbers{
if number > largest{
largest = number
//swift bug 当在循环中打印语句Xcode异常 //println("largest:",largest)
}
// println("largest:",largest)
}
}
//while循环执行代码块
var n = 2
while n < 20 {
n = n * 2
}
//println(n)
var m = 2
do {
m = m * 2
}while m < 1
//println(m)
//..:表示范围
var firstForLoop = 0
for i in 0..3{
firstForLoop += i
// println(firstForLoop)
}//结果为:0 1 3
//println(firstForLoop)
//...:make a range that includes both values
var firstForLoop1 = 0
for i in 0...3{
firstForLoop1 += i
// println(firstForLoop1)
}//结果为:0 1 3 6
//println(firstForLoop)
var secondForLoop = 0
for var i = 0;i < 3;++i{
secondForLoop += 2
}
//println(secondForLoop)
//func 定义方法
func greet(name:String,day:String)->String{
return "Hello \(name), today is \(day)."
}
//println(greet("zhangsan","friday"))
//通过元组返回多个值
func getGasPrices() -> (Double,Double,Double){
return (2.57,4.32,6.43)
}
//println(getGasPrices())
//定义变化的变量参数列表,放在数组中(只能是相同的数据类型)
func sumOf(numbers: Int...) -> Int{
var sum = 0
for number in numbers {
sum += number
}
return sum
}
//println(sumOf())
//println(sumOf(43,242,12))
//Nested Functions 嵌套
func returneFifteen() -> Int{
var y = 10
func add(){
y += 5
}
add()
return y
}
//println(returneFifteen())
//***Functions are a first-class type
func makeIncrementer() ->(Int -> Int){
func addOne(number:Int) ->Int{
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
//println(increment(7))
//page9:
//****将另以功能包含的方法中
func hasAnyMatches(list: Int[],condition: Int -> Bool) -> Bool{
for item in list{
if condition(item){
// println("true")
return true;
}
}
return false;
}
func lessThanTen(number: Int) -> Bool {
return number < 10
}
var numbers = [20,19,3,12]
hasAnyMatches(numbers,lessThanTen)
//***a closure without a name 匿名方法
numbers.map({
(number: Int) -> Int in
let result = 3 * number
// println(result)
return result
}
)
//. Single statement closures implicitly return the value of their only statement.
numbers.map({number in 3 * number})
//println(numbers.map({number in 3 * number}))
//A closure passed as the last argument to a function can appear immediately after the parentheses.
sort([1,5,3,12,2]) {$0 > $1}
//println(sort([1,5,3,12,2]) {$0 > $1})
//*** Objects and Classes 类***
class Shape{
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides"
}
}
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
//println(shapeDescription)
//用init创建类
//self 相当于Java中的this
//Use deinit to create a deinitializer if you need to perform some cleanup before the object is deallocated.
class NamedShape{
var numberOfSides: Int = 0
var name: String
init(name: String){
self.name = name
}
func simpleDescription() -> String{
return "A shape with \(numberOfSides) sides."
}
}
//Methods on a subclass that override the superclass’s implementation are marked with override
//类的继承
class Square: NamedShape{
var sideLength: Double
//初始化
init(sideLength:Double, name: String){
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
//通过override复写父类的方法
override func simpleDescription() -> String {
return " A square with sides of length \(sideLength)."
}
}
let test = Square(sideLength: 5.3, name: "my test square")
test.area()
test.simpleDescription()