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

DAB

I am working on one of the examples in The Swift Programming Language book related to Optional Protocol Requirements. I have a problem in the following code.

import Foundation

@objc protocol CounterDataSource {
    optional func incrementForCount(count: Int) -> Int
    optional var fixedIncrement: Int { get }
}

@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()

for _ in 1...4 {
    counter.increment()
    println(counter.count)
}

Shouldn't this work? The println() continuously outputs 0, when it should be incrementing by 3s.

rintaro

@objc protocol requires @objc implementation.

In your case:

class ThreeSource: CounterDataSource {
    @objc var fixedIncrement = 3
//  ^^^^^ YOU NEED THIS!
}

without it, Objective-C runtime can't find the property.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I can't get RSA encryption to work with my database

From Dev

I can't get ? instead of %s to work on an insert with python mysqldb

From Dev

What are the requirements for binding data to a combobox in WPF? I can't get it to work

From Dev

I can't get this (simple) loop to work properly

From Dev

I can't get my removeDuplicate method to work?

From Dev

I can't get CSS drop-down menu work

From Dev

Why can't I get HAL support to work in grails 2.3.8?

From Dev

Why can't I get this javascript code work?

From Dev

Why can't I get Google's Python test to work?

From Dev

I can't get my Slick slider to work at all

From Dev

Why can't I get convolution to work properly in MATLAB?

From Dev

Why can't I get `< and `> to work in MacVim?

From Dev

Can't get Google Protocol Buffer to compile

From Dev

Optional chaining not working for optional protocol requirements

From Dev

How can I make a protocol method in swift optional?

From Dev

I can't get phpseclib to work

From Dev

I can't get onclick to work

From Dev

I can't get my class to work in pygame

From Dev

Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

From Dev

I can't get double buffering to work

From Dev

Why can't I get these extensions to work?

From Dev

Why can't I get `< and `> to work in MacVim?

From Dev

Program with Protocol Buffer can't get linked

From Dev

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

From Dev

I can't get onclick to work

From Dev

I can't get my crontab to work

From Dev

How could I find out the requirements on RAMs that can work with a laptop?

From Dev

I can't get my get request to work

From Dev

Alamofire prints "Optional(data)", how can I get rid of "Optional"

Related Related

  1. 1

    I can't get RSA encryption to work with my database

  2. 2

    I can't get ? instead of %s to work on an insert with python mysqldb

  3. 3

    What are the requirements for binding data to a combobox in WPF? I can't get it to work

  4. 4

    I can't get this (simple) loop to work properly

  5. 5

    I can't get my removeDuplicate method to work?

  6. 6

    I can't get CSS drop-down menu work

  7. 7

    Why can't I get HAL support to work in grails 2.3.8?

  8. 8

    Why can't I get this javascript code work?

  9. 9

    Why can't I get Google's Python test to work?

  10. 10

    I can't get my Slick slider to work at all

  11. 11

    Why can't I get convolution to work properly in MATLAB?

  12. 12

    Why can't I get `< and `> to work in MacVim?

  13. 13

    Can't get Google Protocol Buffer to compile

  14. 14

    Optional chaining not working for optional protocol requirements

  15. 15

    How can I make a protocol method in swift optional?

  16. 16

    I can't get phpseclib to work

  17. 17

    I can't get onclick to work

  18. 18

    I can't get my class to work in pygame

  19. 19

    Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

  20. 20

    I can't get double buffering to work

  21. 21

    Why can't I get these extensions to work?

  22. 22

    Why can't I get `< and `> to work in MacVim?

  23. 23

    Program with Protocol Buffer can't get linked

  24. 24

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

  25. 25

    I can't get onclick to work

  26. 26

    I can't get my crontab to work

  27. 27

    How could I find out the requirements on RAMs that can work with a laptop?

  28. 28

    I can't get my get request to work

  29. 29

    Alamofire prints "Optional(data)", how can I get rid of "Optional"

HotTag

Archive