Re: Why doesn't this crash?
Re: Why doesn't this crash?
- Subject: Re: Why doesn't this crash?
- From: Alastair Houghton <email@hidden>
- Date: Mon, 12 Sep 2016 08:53:36 +0100
On 10 Sep 2016, at 12:39, Andreas Falkenhahn <email@hidden> 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@hidden)
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:
This email sent to email@hidden