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

Swift -- protocol、extension

时间:2015-12-08 00:23:44      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

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% requested"

    }

}

var a = SimpleClass()

print(a.simpleDescription)

a.adjust()

print(a.simpleDescription)

 

struct SimpleStructure:ExampleProtocol{

    var simpleDescription:String = "A simple structure"

    mutating func adjust(){

        simpleDescription += " (adjust)"

    }

}

var b = SimpleStructure()

print(b.simpleDescription)

b.adjust()

print(b.simpleDescription)

 

extension Int:ExampleProtocol{

    var simpleDescription:String{

        return "The number is \(self)"

    }

    mutating func adjust(){

        self += 50

    }

}

println(7.simpleDescription)

 

extension String:ExampleProtocol{

    var simpleDescription:String{

        return "My name is \(self)"

    }

    mutating func adjust(){}

}

print("XingXing".simpleDescription)

Swift -- protocol、extension

标签:

原文地址:http://www.cnblogs.com/lianfu/p/5027755.html

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