I want to add a selector into a dictionary (the main purpose is for identifying the callback method and delegate after finish doing something)
But I find that I can not do that, the program will get an error “EXC_BAD_ACCESS”.
Are there any other way for me to add that method selector to a dictionary?
Thanks for your help.
Adding a new entry to a dictionary does two things (in addition to adding it to the dictionary, obviously):
NSCopyingprotocolretains the value. This means that it needs to implement theNSObjectprotocolIt’s probably the second that’s causing your
EXC_BAD_ACCESS.There are at least two ways around this.
Firstly, rather than adding the selector you could add the instance of the class that implements the selector to your dictionary. Usually your class will inherit from
NSObjectand it will work fine. Note that it will retain the class though, maybe not what you want.Secondly, you can convert a selector to a string (and back again) using
NSSelectorFromStringandNSStringFromSelector(docs are here).