Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Can';不读取文件_Objective C_Xcode_Macos - Fatal编程技术网

Objective c Can';不读取文件

Objective c Can';不读取文件,objective-c,xcode,macos,Objective C,Xcode,Macos,下面是我读取文件内容的方法。不幸的是,这对我不起作用。文件的路径是正确的。我想念什么 - (IBAction)readFile:(id)sender { NSString *str = [NSString stringWithContentsOfFile:@"/Users/joe/text.txt" encoding:NSUTF32StringEncoding error:nil

下面是我读取文件内容的方法。不幸的是,这对我不起作用。文件的路径是正确的。我想念什么

- (IBAction)readFile:(id)sender
{
    NSString *str = [NSString stringWithContentsOfFile:@"/Users/joe/text.txt" 
                      encoding:NSUTF32StringEncoding 
                      error:nil
                    ];
    NSLog(@"%@", str); //Result is null

}

尝试使用
stringWithContentsOfFile:encoding:error:
的内置功能,并给它一个
NSError
,您可以准确地看到错误:

- (IBAction)readFile:(id)sender {
    NSError *readFileError = nil;
    NSString *str = [NSString stringWithContentsOfFile:@"/Users/joe/text.txt" 
                      encoding:NSUTF32StringEncoding 
                      error:&readFileError
                    ];
    if (readFileError) {
        NSLog(@"%@ - %@", [readFileError localizedDescription], [readFileError localizedFailureReason]);
    } else {
        NSLog(@"%@", str);
    }
}

没有NSPath类吗?嗯..我想知道stringWithContentsOfFile:encoding:error:的错误参数是什么?@Legolas,你说的Npath Joseph是什么意思?我不确定。@user979009:Joseph的意思是你应该向错误参数传递一个值,并在尝试读取文件时实际检查错误是什么。(或者更确切地说,不是一个错误,而是一个地址引用。)@Inerdia我明白了!谢谢问题在于编码参数。