objective-cruntimeassociated-object

How to use selector (SEL) with objc_setAssociatedObject?


Can I use somehow parameter of type SEL with objc_setAssociatedObject without converting it to NSString type?

Currently I'm using this code: objc_setAssociatedObject(thirdPartyObject, &kSelectorKey, NSStringFromSelector(selector), OBJC_ASSOCIATION_RETAIN);

Conversion to NSString works, but in feels wrong. There must be more appropriate solution.


Solution

  • OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

    The 3rd parameter needs to be an object type. So, yes, you need that conversion.