iluvcapra iluvcapra - 6 months ago 13
Swift Question

NSPlaceholders and Swift

I'm looking at the

.selection
of an Array Controller and I want to know when the selection is multiple values or none. In ObjC we'd do this by getting the selection as an
id
and checking it's raw equality with the various placeholder markers:
NSMultipleValuesMarker
,
NSNoSelectionMarker
, and
NSNotApplicableMarker
.

But this doesn't work in Swift:

let currentValue = eventsArrayController.selection.valueForKey("enabled")

if let markerVal = currentValue {
if markerVal == NSMultipleValuesMarker {
// this doesn't compile, AnyObject's can't be compared with ==
}
switch markerVal {
case NSNoValueMarker:
// this doesn't compile, an AnyObject is not a valid pattern
}
}


How do I test the value of placeholder markers in Swift?

Answer

In Swift, == is meant to check for value equality (that is, if two objects contain the same data, regardless of whether they share the same memory location or not). Use the === operator to check for reference equality (which tests if two objects share the same memory location).

Comments