Optional chaining not working for optional protocol requirements

Jose Marchena

I'm following this example from Apple and the optional chaining does not work as expected.

There is a protocol with optional property and function:

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

A class that conform to above protocol:

class ThreeSource: CounterDataSource {
  let fixedIncrement = 3
}

And a class with an optional property (dataSource) that conforms to that protocol:

@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
    }
  }
}

Finally when it comes the time to use an instance of Counter, with a non-nil dataSource property, it doesn't behave as expected:

var counter = Counter()
counter.dataSource = ThreeSource()
  for _ in 1...4 {
    counter.increment()
    println(counter.count)
  }

If I'm not wrong and according to the tutorial, we should get printed 3, 6, 9, 12. But I only get 0, 0, 0, 0.

Here is the optional chaining in class Counter that is expected to be assigning the value 3 (due to the fixedIncrement property in ThreeSource) to amount:

        } else if let amount = dataSource?.fixedIncrement {
        count += amount
    }

However this is not working and that branch is not executed.

Is there anything wrong in the code? Or is this maybe a bug?

Price Ringo

It turns out that the website version of the iBook content for The Swift Programming Language (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html) shows the ThreeSource declared with @objc. Either doing this or deriving ThreeSource from NSObject works.

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Optional Chaining Not Working As Expected

From Dev

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

From Dev

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

From Java

Optional chaining (?.) in nashorn

From Dev

Optional Chaining returning an Int

From Dev

Swift 3.0 Optional Chaining

From Dev

Optional Chaining in JavaScript

From Dev

swift optional chaining with cast

From Dev

Optional chaining with Swift strings

From Dev

Chaining Optional.orElseThrow

From Dev

Optional chaining and Array in swift

From Dev

Optional chaining for constructor calls?

From Dev

Is there an equivalent to optional chaining with arithmetic operators?

From Dev

dynamicType of optional chaining not the same as assignment

From Dev

Optional.ofNullable and method chaining

From Dev

Optional Chaining or ternary expression in Swift?

From Dev

Self in protocol always need to be optional?

From Dev

Is optional or required the default for protocol methods?

From Dev

Swift: setting an optional property of a protocol

From Dev

How to call an optional protocol method?

From Dev

Protocol Oriented Programming Optional Functions

From Dev

Optional protocol conforming for iOS classes

From Dev

Swift optional chaining doesn't work in closure

From Dev

Java8 Optional with Function chaining expression

From Dev

Is there something like the swift optional chaining in javascript?

From Dev

Optional chaining used in left side of assignment in Swift

From Dev

How to know where Optional Chaining is breaking?

From Dev

Optional chaining question mark after function name

From Java

How to use optional chaining with array in Typescript?

Related Related

HotTag

Archive