I did all the connections and other settings, and I got my Core-Data version of RaiseMan to work. Unfortunately, that didn’t include the “Begin Edit on Add” capability. I didn’t want (yet) to ruin the purity by adding subclasses; so I’m limited to put any code-based changes onto the NSPerisistentDocument subclass. I ran into the same issues described by me in the “Challenge: Begin Editing on Add” thread.
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard context == &KVOContext else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
return
}
guard let keyPath = keyPath where keyPath == "arrangedObjects", let object = object where object === employees, let change = change else {
print("Couldn't fully extract the method parameters.")
return
}
guard let rawChangeKind = (change[NSKeyValueChangeKindKey] as? NSNumber)?.unsignedIntegerValue, let changeKind = NSKeyValueChange(rawValue: rawChangeKind) else {
print("Couldn't get a (convertible) change kind.")
return
}
switch changeKind {
case .Insertion:
guard let insertedIndices = change[NSKeyValueChangeIndexesKey] as? NSIndexSet else {
print("Couldn't get list of indices of inserted objects.")
return
}
for insertedIndex in insertedIndices {
tableView.editColumn(0, row: insertedIndex, withEvent: nil, select: true)
}
case .Setting:
guard let oldObjects = change[NSKeyValueChangeOldKey] as? [NSManagedObject], let newObjects = change[NSKeyValueChangeNewKey] as? [NSManagedObject] else {
print("Couldn't get the old and/or new list of objects.")
return
}
let oldObjectSet = NSSet(array: oldObjects)
for (index, value) in newObjects.enumerate() {
if !oldObjectSet.containsObject(value) {
tableView.editColumn(0, row: index, withEvent: nil, select: true)
}
}
default:
break
}
}
I can’t extract any useful information about the array controller’s arranged-objects from the observer. I checked the types (by forcing a crash), both the old and new settings of “arrangedObjects” are NSNull instances! There is no actual array (times 2) I can analyze! Right now, I stop with the “Couldn’t get the old and/or new list of objects.” message. I guess I have to give up this line of coding (and/or skip to the next chapter).