码迷,mamicode.com
首页 > 编程语言 > 详细

swift 导读

时间:2014-12-30 00:25:32      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

// Playground - noun: a place where people can play

import UIKit

/***********************************************************/
/**
* 一. A Swift Tour
*/

//1.hello world
var str = "Hello, playground"
println("Hello, world \(str)")

//2.返回值为函数的函数
func addOne(number: Int) -> Int{
    return 1 + number
}

func makeIncrementer() -> (Int -> Int){
    return addOne
}
var increment = makeIncrementer()
increment(7)

//3.参数为函数的函数
func hasAnyMatches(list: [Int], lessThanTen: Int -> Bool) -> Bool{
    for item in list{
        if lessThanTen(item){
            return true
        }
    }
    return false
}

func lessThanTen(number: Int) -> Bool{
    return number < 10
}

var numbers = [20, 19, 9, 12]
hasAnyMatches(numbers, lessThanTen)

//4.函数是一种特殊的闭包,除了大挂号,也可以使用in隔开闭包名和闭包的内容
numbers.map({
    (number: Int)-> Int in
    let result = 3 * number
    return result
})

//4.1 当闭包的类型已知时,闭包可以简写。
numbers.map({ number in 3 * number})
sort(&numbers) { $0 > $1 }

/***********************************************************/
/**
*  类对象
*/

//1.定义一个类
class Sharp: NSObject {
    var numberOfSides: Int = 0
    var name: String
    
    init(name: String){//构造方法
        self.name = name
    }
    func simpleDescription() -> String {
        return "A sharp with \(numberOfSides) sides"
    }
}

//2.继承, init, override, get, set
class EquilateralTriangle: Sharp {//等边三角形
    var sideLength : Double
    
    //3.init方法
    init(name: String, sideLength: Double) {
        self.sideLength = sideLength
        super.init(name: name)
        numberOfSides = 3
    }
    
    //5.get方法和set方法
    var perimeter: Double{//周长
        get{
            return 3 * sideLength
        }
        set{
            sideLength = newValue / 3.0
        }
    }
    
    func area() -> Double{
        return sideLength * sideLength
    }
    
    //4.覆盖
    override func simpleDescription() -> String {
        return "A EquilateralTriangle with sides of length \(sideLength)."
    }
}

//6. willSet, didSet
class Square: Sharp{
    var sideLength: Double
    init(name: String, length: Double) {
        sideLength = length
        super.init(name: name)
        numberOfSides = 4
    }
    override func simpleDescription() -> String {
        return "A Square with sides of length \(sideLength)."
    }
}

class traingleAndSquare: Sharp {
    var traingle: EquilateralTriangle{
        willSet{
            square.sideLength = newValue.sideLength
        }
    }
    var square: Square{
        willSet{
            traingle.sideLength = newValue.sideLength
        }
    }
    init(size: Double, name: String){
        square = Square(name: name, length: size)
        traingle = EquilateralTriangle(name: name, sideLength: size)
        super.init(name: name)
    }
    
}
//
// Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

//定义一个协议
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 simpleDisc = a.simpleDescription

//扩展
extension Int: ExampleProtocol {
    var simpleDescription: String {
        return "The number \(self)"
    }
    mutating func adjust() {
        self += 42
    }
}

//给Double类型扩展一个绝对值属性
extension Double {
    var absoluteValue :Double {
        return self > 0 ? self : -1 * self
    }
}

var d: Double = -203.1
println(d.absoluteValue)

//范型(Generics)
enum OptionalValue<T> {//可选值
    case None
    case Some(T)
}

var possibleInt: OptionalValue<Int> = .None//OptionalValue.None
possibleInt = .Some(100)//OptionalValue.Some(100)

 


 

 

swift 导读

标签:

原文地址:http://www.cnblogs.com/actionke/p/4192643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!