Swift 3: queryRow.key returning a key of (function)


#1

Xcode 8, Swift 3.

The issue is that after emitting a few key value pairs, and then running a query against that view, the value’s are correct but the keys are returning (function)… kind of.

Here’s a snippet of the emit

            let k = doc["emp_id"] as! String
            let v = doc["emp_name"] as! String
            print("emitting key = \(k)   value = \(v)")
            emit(k, v)

which prints

emitting key = E001 value = Company 1
emitting key = E002 value = Company 2
emitting key = E003 value = Company 3

Then a very simple query

        let cblQuEnum = try query.run()
        while let queryRow = cblQuEnum.nextRow() { 
            print("key         = \(queryRow.key)   value = \(queryRow.value!)")
            print("queryRow    = \(queryRow)")
            let x = queryRow.value(forKey: "key")
            print("valueForKey = \(x!)\n")
        }

and the output

key = (Function) value = Company1
queryRow = CBLQueryRow[key=“E001”; value=“Company1”; id=-DnBbrZYkM_LAjqxPWhSuGX]
valueForKey = E001

key = (Function) value = Company2
queryRow = CBLQueryRow[key=“E002”; value=“Company2”; id=-ltj_-77Mf5PX6ZMrTIZ49Y]
valueForKey = E002

key = (Function) value = Company3
queryRow = CBLQueryRow[key=“E003”; value=“Company3”; id=-JgtI9UCkjr7SYEo98upg1A]
valueForKey = E003

So, the queryRow contains key=E001 etc, so that checks.
valueForKey = E001, so that’s ok

queryRow.key is a (function)?

Any thoughts as to why queryRow.key is a function when obtaining that other ways is a String?


#2

We noticed this last week while updating sample code. This appears to be caused by a bug in the way Swift 3 maps Obj-C method names, and I’ve filed a ticket at Swift.org. We do need to find a workaround, though.

What’s happening is that there’s a method’ -[CBLQuery keyAtIndex:], whose name now gets mapped to simply ‘key’. So the class’s Swift API ends up with both a property and a method named ‘key’, and worse, the method seems to shadow the property.


#3

As a quick workaround, edit CBLQuery.h and comment out the -keyAtIndex method of CBLQueryRow. (It’s an obscure convenience method you won’t need.)


#4

Here’s the Swift bug I filed — no progress on fixing it yet, apparently.


#5

Thank you for the input and the update.

While editing the CBLQuery.h file is an option, if we replace it in the future or of there’s an update to CouchBase it would break our code.

Instead, is this safe®?

let x = queryRow.value(forKey: “key”)


#6

That should work, it’s just slower.


#7

We did the same thing as a workaround in our sample code.