objective-c - BOOL 类型指针的初始化警告

标签 objective-c xcode

我从一个常量 bool 表达式中收到一条新警告,提示类型为“BOOL *”(又名“bool *”)的指针初始化为 null。这是导致我以前从未见过的警告的代码?

if ([[NSFileManager defaultManager]fileExistsAtPath:filePath isDirectory:NO]) {

最佳答案

fileExistsAtPath:isDirectory: 方法中,isDirectory 是一个按引用返回的参数,即它返回一个 bool 值,指示路径是否为目录。

来自 Apple 文档:

isDirectory: Upon return, contains YES if path is a directory or if the final path element is a symbolic link that points to a directory, otherwise contains NO. If path doesn’t exist, this value is undefined upon return. Pass NULL if you do not need this information.

使用:

BOOL isDirectory;
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath isDirectory:& isDirectory]) {
    if (isDirectory) {
        // Code for the directory case
    }
    else {
        // Code for the file case
    }
    ...
}

如果您不想知道路径是否指向目录,只需使用:

- (BOOL)fileExistsAtPath:(NSString *)path

关于objective-c - BOOL 类型指针的初始化警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203288/

相关文章:

iphone - SDK错误编译错误

ios - Sprite 套件碰撞 : ignore transparency?

ios - webview 不显示在 iphone 6 的全屏上

iphone - 如何只运行一次代码?

ios - ( objective-c )方法和变量如果只在自己的类中使用,是否需要在头文件(.h)中声明?

ios - XCTAssertEqualWithAccuracy 不工作

ios - 如何从 Parse 中删除对象?

iphone - 核心数据——如何插入数组

objective-c - 在设置值之前等待现有的 iCloud 值?

ios - 最新的 Google Plus iOS SDK 1.7.1 不支持架构 x86_64