我通过在运行时 Select 最新的API来支持10.4+:

if ([fileManager respondsToSelector:@selector(removeItemAtPath:error:)])
    [fileManager removeItemAtPath:downloadDir error:NULL];
else
    [fileManager removeFileAtPath:downloadDir handler:nil];

在这种情况下,10.5及以上将使用removeItemAtPath:error:,10.4将使用removeFileAtPath:handler:.很好,但我仍然会收到针对旧方法的编译器警告:

warning: 'removeFileAtPath:handler:' is deprecated [-Wdeprecated-declarations]

是否有if([… respondsToSelector:@selector(…)]){ … } else { … }的语法提示编译器(叮当声)不要在那一行发出警告?

如果没有,有没有一种方法可以将该行标记为忽略-Wdeprecated-declarations


在看到一些答案之后,让我澄清一下,让编译器不知道我在做什么并不是一个有效的解决方案.

推荐答案

我在Clang Compiler用户手册中找到了an example,可以忽略警告:

if ([fileManager respondsToSelector:@selector(removeItemAtPath:error:)]) {
    [fileManager removeItemAtPath:downloadDir error:NULL];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [fileManager removeFileAtPath:downloadDir handler:nil];
#pragma clang diagnostic pop
}

Objective-c相关问答推荐

警告:格式字符串不是字符串文字(可能不安全)

通过指针算术访问数组值与 C 中的下标

按住自定义 UIButton 时更改深灰色突出显示的 colored颜色 ?

在 viewDidAppear 之前无法正确设置框架

将长格式的 NSString 拆分为多行

NSManagedObjectContext performBlockAndWait:不在后台线程上执行?

静态 NSString 使用与内联 NSString 常量

didReceiveRemoteNotification:当应用程序处于后台并且未连接到Xcode时未调用fetchCompletionHandler

如何从 Xcode4 生成 UML 图

dispatch_semaphore_dispose 上的 EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

在 Objective-C/cocoa 中创建文件夹/目录

如何测试生产推送通知?

奇怪的错误 NSAssert

Cocoa 的依赖注入框架?

如何在 Objective-C 中将 NSString 解析为 BOOL?

检测 UITableView 滚动

Instruments Allocations 跟踪用户定义类的对象的分配和解除分配

UITableView ,重新加载时滚动到底部?

Xcode没有编译任何项目? 'clang 失败,退出代码 255'

在 iPhone 上查找用户的 Documents 目录的最佳方法是什么?