Reputation: 32091
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.
Upvotes: 3
Views: 261
Reputation: 540055
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 theNSMutableSet
primitive 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 eachNSMutableSet
message 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.
Upvotes: 2