加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:13:06 所属栏目:百科 来源:网络整理
导读:我正在从一个常量布尔表达式获得一个新的警告,即将’BOOL *'(又名’bool *’)类型的指针初始化为null.这是导致我以前没见过的警告的代码? if ([[NSFileManager defaultManager]fileExistsAtPath:filePath isDirectory:NO]) { 解决方法 在方法fileExistsAtPa
我正在从一个常量布尔表达式获得一个新的警告,即将’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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读