Weak-linking a class
-
How do I test for the existence of a Cocoa class at runtime that could be absent on an earlier system?
The situation is that I want to use NSCache instead of NSMutableDictionary somewhere. The object is created as a singleton when it's first used, but I can't quite see how I should decide which class to make. I've read the weak-linking guide and it makes sense, but the example in there shows only a simple C function, not an Obj-C class.
I'm setting 10.6 SDK as my base SDK, but my minimum deployment target is 10.5. Therefore NSCache will not be there on 10.5.
I thought I could do e.g.:
if([NSCache alloc] == nil ){ ... fall back to NSMutableDictionary ... }
but on 10.6 that actually calls [NSCache alloc] which I don't want, I just want to test whether [NSCache alloc] (or any appropriate class method) actually exists or not, or more precisely, whether NSCache exists. I'm guessing it's easy and probably obvious when you know it, but I'm not seeing it.
--Graham -
NSClassFromString()? Or maybe some objc_xxx runtime function?
--Andy
On Jun 27, 2010, at 9:50 AM, Graham Cox wrote:
> How do I test for the existence of a Cocoa class at runtime that could be absent on an earlier system?
>
> The situation is that I want to use NSCache instead of NSMutableDictionary somewhere. The object is created as a singleton when it's first used, but I can't quite see how I should decide which class to make. I've read the weak-linking guide and it makes sense, but the example in there shows only a simple C function, not an Obj-C class.
>
> I'm setting 10.6 SDK as my base SDK, but my minimum deployment target is 10.5. Therefore NSCache will not be there on 10.5.
>
> I thought I could do e.g.:
>
> if([NSCache alloc] == nil ){ ... fall back to NSMutableDictionary ... }
>
> but on 10.6 that actually calls [NSCache alloc] which I don't want, I just want to test whether [NSCache alloc] (or any appropriate class method) actually exists or not, or more precisely, whether NSCache exists. I'm guessing it's easy and probably obvious when you know it, but I'm not seeing it.
>
> --Graham
-
On 27/06/2010, at 11:55 PM, Andy Lee wrote:
> NSClassFromString()? Or maybe some objc_xxx runtime function?
>
> --Andy
D'oh, of course. Just tired... ;-)
--Graham -
See also <
http://www.sealiesoftware.com/blog/archive/2009/09/09/objc_explain_Weak-imp
ort_classes.html
> .
-Ken
On Sun, Jun 27, 2010 at 7:02 AM, Graham Cox <graham.cox...> wrote:
>
> On 27/06/2010, at 11:55 PM, Andy Lee wrote:
>
>> NSClassFromString()? Or maybe some objc_xxx runtime function?
>>
>> --Andy
>
>
> D'oh, of course. Just tired... ;-)
>
> --Graham
>


