switf基础教程2

此教程主要是依据为苹果官方文档,部分代码使用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()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值