On 10 Sep 2016, at 12:39, Andreas Falkenhahn <[email protected]> wrote:
> 
> To open a video, I do the following:
> 
>    AVPlayer *p = [[AVPlayer alloc] initWithURL:url];
> 
> I'd expect this code to crash on 10.6 because 10.6 doesn't have AVPlayer.
> To my surprise, however, the code doesn't crash and it just returns NULL.

[snip]

> However, I'm wondering whether it is ok to execute this code on 10.6 without
> any safeguard. I thought I'd have to do something like this instead:
> 
>    if(floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_7) {

Testing against version numbers should be a last resort; it’s considered good 
practice, generally speaking, to explicitly test for the feature you’re after 
instead of doing that.  In this instance, you’re weakly linked against the 
framework, so just doing

  AVPlayer *p = [[AVPlayer alloc] initWithURL:url];

is absolutely fine; it will return nil because sending messages to nil is 
defined to do that in Objective-C.

Sometimes you might want to test for a particular method rather than a class, 
and you can use -respondsToSelector() for that.

(I’ll add that in a general sense, it makes no sense testing the AppKit version 
number to see whether AVFoundation is available.  While the Objective-C part of 
your program *probably* only targets Mac OS X and this will probably work for 
you, should you ever want to use the same code with e.g. GNUStep or Cocotron, 
you’ll be out of luck.)

Kind regards,

Alastair.

--
http://alastairs-place.net


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to