繁体   English   中英

ARC不允许将'int'隐式转换为'NSDictionary *'

[英]Implicit conversion of 'int' to 'NSDictionary *' is disallowed with ARC

使用NSFileManager-attributeOfItemAtPath时,出现错误“不允许将'int'隐式转换为'NSDictionary *'的错误”。

我想知道文件大小时,我猜发生了一些错误。 有什么方法可以解决这个问题?

    #import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *fName = @"testfile";
        NSFileManager *fm;
        NSDictionary *attr;


        fm = [NSFileManager defaultManager];


        if([fm fileExistsAtPath:fName]==NO){
            NSLog(@"File doesnt exist!");
            return 1;
        }


        if([fm copyItemAtPath:fName toPath:@"newfile" error:nil]==0){
            NSLog(@"Files copy Failed!");
            return 2;
        }


        if([fm contentsEqualAtPath:@"newfile" andPath:@"newfile2"] == NO){
            NSLog(@"File arent equal");
            return 3;
        }


        if([fm moveItemAtPath:@"newfile" toPath:@"newfile2" error:nil] == NO){
            NSLog(@"file rename failed");
            return 4;
        }

        //Here
        if((attr = [fm attributesOfItemAtPath:@"newfile2" error:nil] == NO)){

            return 5;
        }

    }
    return 0;
}

谢谢

您正在使用的其他方法: moveItemAtPathcontentsEqualAtPathcopyItemAtPathfileExistsAtPath它们都返回BOOL 但是attributesOfItemAtPath返回NSDictionary并且您不能将NSDictionaryBOOL进行比较( NO

试一试

((attr = [fm attributesOfItemAtPath:@"newfile2" error:nil]) == nil)

您将对具有整数类型NO (整数)的字典实例的引用进行比较。 您可能想检查nil 此外,您应该在左侧使用括号,否则编译器会尝试将[…]==NO的结果赋给属性:

if( (attr = [fm attributesOfItemAtPath:@"newfile2" error:nil]) == nil) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM