Protocols and Extensions swift -
i new swift , learning swift "the swift programming language(swift 3 beta)". below simple example book of protocol extension
protocol exampleprotocol { var simpledescription: string {get} mutating func adjust() } class simpleclass: exampleprotocol { var simpledescription: string = "a vert simple class." var anotherproperty: int = 69105 func adjust() { simpledescription += "now 100% adjusted." } } var = simpleclass() a.adjust() let adescripition = a.simpledescription struct simplestructure: exampleprotocol { var simpledescription: string = "a simple structure" mutating func adjust() { simpledescription += "(adjusted)" } } var b = simplestructure() b.adjust() let bdescription = b.simpledescription extension int: exampleprotocol{ var simpledescription : string { return "the number \(self)" } mutating func adjust() { self += 42 } } //var c = simpleclass() //c.adjust() print(7.simpledescription)
the end print result "the number 7\n". can see in extension, have mutating function add 42 self. question how can call mutating function in extension result added value of +42.
how call mutating function in extension result added value of +42.?
the function results in added value of +42 adjust()
function of int. call it, run this:
var c = 7 c.adjust() print(c)
c
49 (7 + 42)
Comments
Post a Comment