Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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 获取N错误代码中的错误_Objective C - Fatal编程技术网

Objective c 获取N错误代码中的错误

Objective c 获取N错误代码中的错误,objective-c,Objective C,我在这段代码中遇到错误;我已将错误消息放在评论中。我想不出来 提前谢谢 #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { NSMutableString *str = [[NSMutableString alloc]init]; for (int i = 0; i < 10; i++) {

我在这段代码中遇到错误;我已将错误消息放在评论中。我想不出来

提前谢谢

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        NSMutableString *str = [[NSMutableString alloc]init];
        for (int i = 0; i < 10; i++) {
        [str appendString:@"Aaron is cool!\n"];
        }

        // Declare a pointer to an NSError object, but don't instantiate it.
        // The NSError instance will only be created if there is, in fact, an error.
        NSError *error = nil;

        // Pass the error pointer by reference to the NSString method
        BOOL success =[str writeToFile:@"/tmp/cool.txt"; // Expected "]"
          atomically:YES // Bad receiver type 'int'
            encoding:NSUTF8StringEncoding 
               error:&error];

        // Test the returned BOOL, and query the NSError if the write failed
        if (success) {
        NSLog(@"done writing /tmp/cool.txt");
        } else {
        NSLog(@"writing /tmp/cool/txt failed:@", error localizedDescription); //    Expected ')'
        }

    }
    return 0;
}
#导入
int main(int argc,const char*argv[]
{
@自动释放池{
NSMutableString*str=[[NSMutableString alloc]init];
对于(int i=0;i<10;i++){
[str appendString:@“亚伦很酷!\n”];
}
//声明指向NSError对象的指针,但不要实例化它。
//只有在事实上存在错误时,才会创建NSError实例。
n错误*错误=nil;
//通过引用将错误指针传递给NSString方法
BOOL success=[str writeToFile:@”/tmp/cool.txt;//应为“]
原子性:是//接收器类型“int”错误
编码:NSUTF8StringEncoding
错误:&错误];
//测试返回的BOOL,如果写入失败,则查询NSError
如果(成功){
NSLog(@“完成写入/tmp/cool.txt”);
}否则{
NSLog(@“写入/tmp/cool/txt失败:@”,错误本地化描述);//预期''
}
}
返回0;
}

如果您的代码没有任何打字错误,这就是问题所在

// Pass the error pointer by reference to the NSString method
        BOOL success =[str writeToFile:@"/tmp/cool.txt"; // Expected "]"
          atomically:YES // Bad receiver type 'int'
            encoding:NSUTF8StringEncoding 
               error:&error];
从这里删除分号“;”

BOOL success =[str writeToFile:@"/tmp/cool.txt"; // Expected "]"
试试这个:

@autoreleasepool {
    NSMutableString *str = [[[NSMutableString alloc]init] autorelease];
    for (int i = 0; i < 10; i++) {
        [str appendString:@"Aaron is cool!\n"];
    }
    NSError *error = nil;
    BOOL success =[str writeToFile:@"/tmp/cool.txt"
                        atomically:YES
                          encoding:NSUTF8StringEncoding 
                             error:&error];
    if (success) {
        NSLog(@"done writing /tmp/cool.txt");
    } else {
        NSLog(@"writing /tmp/cool/txt failed: %@", [error localizedDescription]); 
    }

}
return 0;
@autoreleasepool{
NSMutableString*str=[[[NSMutableString alloc]init]自动释放];
对于(int i=0;i<10;i++){
[str appendString:@“亚伦很酷!\n”];
}
n错误*错误=nil;
BOOL success=[str writeToFile:@/tmp/cool.txt”
原子论:是的
编码:NSUTF8StringEncoding
错误:&错误];
如果(成功){
NSLog(@“完成写入/tmp/cool.txt”);
}否则{
NSLog(@“写入/tmp/cool/txt失败:%@,[error localizedDescription]);
}
}
返回0;