检查 C 枚举是否存在
在使用之前如何检查 NSRegularExpressionSearch 是否存在?
enum {
NSCaseInsensitiveSearch = 1,
NSLiteralSearch = 2,
NSBackwardsSearch = 4,
NSAnchoredSearch = 8,
NSNumericSearch = 64,
NSDiacriticInsensitiveSearch = 128,
NSWidthInsensitiveSearch = 256,
NSForcedOrderingSearch = 512,
NSRegularExpressionSearch = 1024
};
更新-我想针对最新的 SDK 进行编译并在运行时检查 NSRegularExpressionSearch 是否存在。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSRegularExpressionSearch
仅在以下情况下编译,因此您需要检查当前操作系统是否为 3.2 或更高版本。
在其他情况下,您可能会检查类是否存在或实例是否响应选择器,但 NSString 除了该枚举之外没有更改。例如,如果有一个与手势识别器关联的枚举,您可以使用以下之一:
对于另一个示例,请参阅 Apple 如何处理 UI_USER_INTERFACE_IDIOM 宏。
编辑:
除了系统版本之外,要检查的版本号是
NSFoundationVersionNumber
。这与 NSString 联系更紧密,但 3.2 标头中没有 3.2 的常量。
NSRegularExpressionSearch
is only compiled whenSo you need to check that the current operating system is 3.2 or later.
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:
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
.That is more closely tied to NSString, but there is no constant for 3.2 in the 3.2 headers.
问题标题不正确。您的问题不是 NSRegularExpressionSearch 是否存在。 (是的,它在 SDK >= 3.2 的编译时存在。)您的问题是像
rangeOfString:options:
这样的方法是否可以在运行时正确解释正则表达式的选项位。由于这纯粹是一个关于函数行为的问题,因此解决这个问题的一个明显方法就是做一个实验。做一些你知道在有支持时会成功但在没有支持时会失败的事情。
我们可以尝试使用与字符串匹配的正则表达式进行匹配,但是正则表达式字符串实际上并不存在于字符串中,因此如果它不理解正则表达式选项,它将进行文字匹配并失败。例如,
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,