我正在从一个常量布尔表达式获得一个新的警告,即将’BOOL *'(又名’bool *’)类型的指针初始化为null.这是导致我以前没见过的警告的代码?
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath isDirectory:NO]) {
解决方法
在方法fileExistsAtPath:isDirectory中:isDirectory是一个按引用返回的参数,即它返回一个Bool,指示路径是否是目录.
来自Apple Docs:
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