我在这段代码中遇到错误;我已将错误消息放在评论中。无法理解。
提前致谢。
#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;
}
答案 0 :(得分:1)
如果您的代码没有任何拼写错误,那就是问题
// 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 "]"
答案 1 :(得分: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;