Best way of determining if class is available? (Cocoa)

Discussion in 'Mac Programming' started by ArtOfWarfare, Jun 20, 2012.

  1. ArtOfWarfare, Jun 20, 2012
    Last edited: Jun 20, 2012

    macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    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:

    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.
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    Look up the Foundation function NSClassFromString.
     
  3. macrumors regular

    Joined:
    Apr 8, 2009
    #3
    This should work for deployment targets from 10.6.8:

    Code:
    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.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    As per the Apple documentation on the subject this is the correct way to do it.
     
  5. JoshDC, Jun 21, 2012
    Last edited: Jun 21, 2012

    macrumors regular

    Joined:
    Apr 8, 2009
    #5
    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 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.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Shows what I get for linking something I remember from ages ago without re-reading it :eek:
     
  7. macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #7
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    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.
     

Share This Page