Check if a C enum exists
How would I check that NSRegularExpressionSearch exists before using it?
enum {
NSCaseInsensitiveSearch = 1,
NSLiteralSearch = 2,
NSBackwardsSearch = 4,
NSAnchoredSearch = 8,
NSNumericSearch = 64,
NSDiacriticInsensitiveSe开发者_Python百科arch = 128,
NSWidthInsensitiveSearch = 256,
NSForcedOrderingSearch = 512,
NSRegularExpressionSearch = 1024
};
Update- I want to compile against the latest SDK and check at runtime if NSRegularExpressionSearch exists.
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.
The question title is not correct. Your question is not whether NSRegularExpressionSearch exists. (Yes, it exists at compile time with an SDK >= 3.2.) Your question is whether methods like rangeOfString:options:
can correctly interpret the option bit for regular expression at runtime.
Since this is a question purely about the behavior of a function, one obvious way to figure it out is to do an experiment. Do something that you know will succeed when support is there but will fail when it isn't.
We can attempt a match using a regular expression that matches a string, but where the regex string does not literally exists in the string, so that if it doesn't understand the regex option it will do a literal match and fail. For example,
if ([@"b" rangeOfString:@"." options:NSRegularExpressionSearch].location != NSNotFound) {
// NSRegularExpressionSearch supported
}
精彩评论