我从一个常量 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/