在获取代码NSError错误

我在这段代码收到错误; 我已经把错误消息中的注释。 不能看着办吧。

提前致谢。

#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; }

--------------解决方案-------------

尝试这个:

@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;

如果你的代码没有任何错别字这就是问题所在

// 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 "]"

分类:目标C 时间:2015-03-15 人气:3
本文关键词: 目标C
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.310 (s). 10 q(s)