swift - Optional Protocol Requirements, I Can't Get It To Work -


i working on 1 of examples in the swift programming language book related optional protocol requirements. have problem in following code.

import foundation  @objc protocol counterdatasource {     optional func incrementforcount(count: int) -> int     optional var fixedincrement: int { } }  @objc class counter {     var count = 0     var datasource: counterdatasource?     func increment() {         if let amount = datasource?.incrementforcount?(count) {             count += amount         } else if let amount = datasource?.fixedincrement {             count += amount         }     } }  class threesource: counterdatasource {     var fixedincrement = 3 }  var counter = counter() counter.datasource = threesource()  _ in 1...4 {     counter.increment()     println(counter.count) } 

shouldn't work? println() continuously outputs 0, when should incrementing 3s.

@objc protocol requires @objc implementation.

in case:

class threesource: counterdatasource {     @objc var fixedincrement = 3 //  ^^^^^ need this! } 

without it, objective-c runtime can't find property.


Comments