0

I have recently upgraded to XCode 6.3 and I am trying to initiate an ODQuery as per the Apple Documentation and getting an error.

'Int' is not convertible to 'ODMatchType'

I am using Apple's own Documentation Open Directory Programming Guide

Here is my Swift code:

var err:NSError?
var session = ODSession.defaultSession()
var node = ODNode(session: session, name: "/Local/Default", error: &err)
var query = ODQuery(node: node, forRecordTypes: kODRecordTypeUsers, attribute: kODAttributeTypeRecordType, matchType: kODMatchContains, queryValues: "darren", returnAttributes: kODAttributeTypeStandardOnly, maximumResults: 10, error: &err)?

The issue seems to be that kODMatchContains or any other matchType that are tried are not compatible with the Swift 1.2 function?

If someone knows what is going on or if you can try the code in 6.3 yourself and get the same results, please let me know.

Ecks
  • 103
  • 1
  • 8

1 Answers1

1

This has nothing to do with Swift 1.2. It was always this way in Swift. The matchType: parameter needs to be an ODMatchType. Coerce it to an ODMatchType, like this:

... matchType: ODMatchType(kODMatchContains), ...

And that line will then compile just fine (once you remove the question mark at the end).

Apparently I have to spell it out for you...

var err:NSError?
var session = ODSession.defaultSession()
var node = ODNode(session: session, name: "/Local/Default", error: &err)
var query = ODQuery(node: node, forRecordTypes: kODRecordTypeUsers, attribute: kODAttributeTypeRecordType, matchType: ODMatchType(kODMatchContains), queryValues: "darren", returnAttributes: kODAttributeTypeStandardOnly, maximumResults: 10, error: &err)
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This is really no different from my complaint here: http://stackoverflow.com/questions/24108827/swift-numerics-and-cgfloat-cgpoint-cgrect-etc ODMatchType is UInt32, but `kODMatchContains` is an Int. Therefore you can't use the one where the other is expected, because there is no implicit coercion between numeric types; you must coerce _explicitly_ to the expected type. It is especially maddening when the API appears to be willfully handing you the wrong type to begin with. – matt Apr 19 '15 at 02:22
  • Yes, that is what the documentation states but this produces an error in all of my tests. – Ecks Apr 19 '15 at 02:24
  • It produces a _different_ error. I answered the question you asked. Obviously then you must remove that weird question mark at the end of the whole thing. What it was ever doing there is a mystery... – matt Apr 19 '15 at 02:27
  • I am sorry, you are correct. It has fixed my problem. I have never seen that before. When I first read your code here it wasn't clear. I like it. Thank you. Do you know where this is documented by Apple? I couldn't find this answer anywhere? – Ecks Apr 19 '15 at 02:52
  • @Ecks There is nothing to "document", really. It's just how Swift works. My book on Swift discusses the matter: http://www.apeth.com/swiftBook/ch03.html#_coercion – matt Apr 19 '15 at 03:05