[swift-users] Method of same name is shadowing a property in Swift 3

Jens Alfke jens at mooseyard.com
Tue Sep 13 15:13:18 CDT 2016

One of my co-workers just noticed a problem with the (bridged) API of our Objective-C framework, when used from Swift 3 (but not earlier). We have a class CBLQueryRow that includes the following:

	@property (readonly) id key;
	- (nullable id) keyAtIndex: (NSUInteger)index;

In Swift this becomes
	open var key: Any { get }
	open func key(at index: UInt) -> Any?

The problem is that any reference to `key` now seems to refer to a pointer to the method, not to the property, leading to compiler diagnostics like the following (where `row` is a CBLQueryRow):

TaskListsViewController.swift:95:40: warning: cast from '(UInt) -> Any?' to unrelated type 'String' always fails
        cell.textLabel?.text = row.key as? String
                               ~~~~~~~ ^   ~~~~~~
That’s a warning not an error, but obviously at runtime the `as?` would always fail and result in nil.

This is rather bad for us, since the `key` property is a crucial part of the API, while the `key()` method that pre-empts it is obscure and little-used.

I can reproduce this in a playground in Xcode 8 like so:

protocol QueryRow {
    var key: Any { get }
    func key(at index: UInt) -> Any?

var row: QueryRow

row.key as? String
In addition to the valid error about `row` being uninitialized, I also get the cast failure.

What’s the best workaround for this?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160913/732acd76/attachment.html>

More information about the swift-users mailing list