views:

86

answers:

1

How would I check that NSRegularExpressionSearch exists before using it?

enum {
  NSCaseInsensitiveSearch = 1,
  NSLiteralSearch = 2,
  NSBackwardsSearch = 4,
  NSAnchoredSearch = 8,
  NSNumericSearch = 64,
  NSDiacriticInsensitiveSearch = 128,
  NSWidthInsensitiveSearch = 256,
  NSForcedOrderingSearch = 512,
  NSRegularExpressionSearch = 1024
};

Update- I want to compile against the latest SDK and check at runtime if NSRegularExpressionSearch exists.

+2  A: 

NSRegularExpressionSearch is only compiled when

#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED

So you need to check that the current operating system is 3.2 or later.

if ( [[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2 ) {}

In other cases you might check that a class exists or that an instance responds to a selector, but NSString did not change other than that enum. For example, if there was an enum associated with gesture recognizers you could use one of the following:

if ( NSClassFromString( @"UIGestureRecognizer" ) != nil ) {}
if ( [someView respondsToSelector:@selector(gestureRecognizers)] ) {}

For another example, see how Apple handles the UI_USER_INTERFACE_IDIOM macro.

Edit:

A version number to check besides the system version is NSFoundationVersionNumber.

if ( NSFoundationVersionNumber > NSFoundationVersionNumber_iPhoneOS_3_1 ) {}

That is more closely tied to NSString, but there is no constant for 3.2 in the 3.2 headers.

drawnonward
I was trying to avoid checking by OS version, as that is usually a bad practice. But it looks like the only way to check.
christo16