Search code examples
objective-cclass-methodobjective-c-category

Call class method of Objective C category


AFNetworking has a class method called + af_sharedImageCache that I want to access from my own category, but I can't figure out the syntax to reference this. Internally it is doing [[self class] af_sharedImageCache] but obviously that's not gonna work for me. :)


Solution

  • That's because + af_sharedImageCache is a private method, not exposed on UIImageView+AFNetworking.h. You can call it, though, using Obj-C runtime.

    if ([[self class] respondsToSelector:@sel(af_sharedImageCache)]) {
         NSCache *cache = [[self class] performSelector:@sel(af_sharedImageCache)];
    }
    

    However, AFImageCache is a private class, and you would have to do the same hack to use its methods. If I were you, I'd create my own cache, as it's clear that AFNetworking doesn't want that you mess with its cache implementation.