PDA

View Full Version : Best way of determining if class is available? (Cocoa)




ArtOfWarfare
Jun 20, 2012, 10:17 PM
Hi,

What's the best way of determining if a certain feature is available?

I know you can check whether certain methods of a class exist, but that requires the class itself to exist, does it not? How can I determine if that class exists?

Right now, I'm thinking I'll try using gestaltSystemVersionMinor, but there's a pretty clear note in the documentation against doing that:

Important Never infer the availability of hardware or software features from the value that the Gestalt function returns when you pass it the gestaltSystemVersion selector.

Specifically, I'm wondering about (User) Notification Center. If it's available, I'd like to use it; if it's not available, I'd like to fall back on alert windows.



chown33
Jun 20, 2012, 11:56 PM
Look up the Foundation function NSClassFromString.

JoshDC
Jun 21, 2012, 05:49 AM
This should work for deployment targets from 10.6.8:

if ([NSUserNotificationCenter class]) {

}

Documentation on it seems quite sparse, but is the preferred method of checking class availability. The best explanation of it I've found is in the WWDC 2011 session on Objective-C advancements.

robbieduncan
Jun 21, 2012, 06:52 AM
This should work for deployment targets from 10.6.8:

if ([NSUserNotificationCenter class]) {

}

Documentation on it seems quite sparse, but is the preferred method of checking class availability. The best explanation of it I've found is in the WWDC 2011 session on Objective-C advancements.

As per the Apple documentation on the subject (https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-SW3) this is the correct way to do it.

JoshDC
Jun 21, 2012, 07:29 AM
As per the Apple documentation on the subject (https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-SW3) this is the correct way to do it.

That document is out of date, and explicitly says it's not possible with OS X:

In Mac OS X (and in iOS projects that do not meet the set of conditions just listed), you cannot use the class method to determine if a weakly linked class is available.

The only mention in official documentation I could find was the 10.7 release notes (https://developer.apple.com/library/mac/#releasenotes/Cocoa/Foundation.html) which doesn't really provide any useful details. I've sent feedback on the document you linked to, so will hopefully it will be updated.

robbieduncan
Jun 21, 2012, 07:41 AM
That document is out of date, and explicitly says it's not possible with OS X:



The only mention in official documentation I could find was the 10.7 release notes (https://developer.apple.com/library/mac/#releasenotes/Cocoa/Foundation.html) which doesn't really provide any useful details. I've sent feedback on the document you linked to, so will hopefully it will be updated.

Shows what I get for linking something I remember from ages ago without re-reading it :o

Littleodie914
Jun 21, 2012, 11:41 AM
This is the standard, preferred way of doing it:

Class klass = NSClassFromString(@"NSUserNotificationCenter");
if (klass) {
// Bingo
} else {
// Nope
}

Edit: As mentioned above, there is new support for using the

[NSUserNotificationCenter class]

method, but it is a newer feature and is not supported on all versions of OS X / iOS:

https://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iOS4.html#//apple_ref/doc/uid/TP40010313-SW5

gnasher729
Jun 21, 2012, 12:54 PM
This is the standard, preferred way of doing it:

Class klass = NSClassFromString(@"NSUserNotificationCenter");
if (klass) {
// Bingo
} else {
// Nope
}

Edit: As mentioned above, there is new support for using the

[NSUserNotificationCenter class]

method, but it is a newer feature and is not supported on all versions of OS X / iOS:

https://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iOS4.html#//apple_ref/doc/uid/TP40010313-SW5

It should really work everywhere. [NSUserNotificationCenter class] attempts to send the class method named "class" to the object NSUserNotificationCenter. Because the object doesn't exist, it will be nil, and sending a message to a nil object returns nil reliably.