class TestClass : NSObject {
var definitions: NSSet = NSSet()
func addDefinitionsObject(value: AnyObject) {
self.mutableSetValueForKey("definitions").addObject(value)
}
func removeDefinitionsObject(value: AnyObject) {
// this method is never called
self.mutableSetValueForKey("definitions").removeObject(value)
}
}
var test = TestClass()
test.addDefinitionsObject("yo")
Running this causes infinite recursion, which ultimately crashes with EXC_BAD_ACCESS. Any ideas why this is happening?
What's weird is that this only happens if removeDefinitionsObject is defined. If I remove that function, the issue goes away.
From "Accessor Search Pattern for Unordered Collections" in the "Key-Value Coding Programming Guide" (emphasis mine):
The default search pattern for
mutableSetValueForKey:is as follows:
- Searches the receiver's class for methods whose names match the patterns
add<Key>Object:andremove<Key>Object:(corresponding to theNSMutableSetprimitive methodsaddObject:andremoveObject:respectively) and alsoadd<Key>:andremove<Key>:(corresponding to NSMutableSet methods unionSet: and minusSet:). If at least one addition method and at least one removal method are found eachNSMutableSetmessage sent to the collection proxy object will result in some combination ofadd<Key>Object:,remove<Key>Object:,add<Key>:, andremove<Key>:messages being sent to the original receiver ofmutableSetValueForKey:.- ...
Which means that if both addDefinitionsObject and removeDefinitionsObject
are implemented in your class, then
self.mutableSetValueForKey("definitions").addObject(value)
calls
self.addDefinitionsObject(value)
hence the recursion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With