Objective-C 文件 I/O 错误
- (IBAction)sendMessage:(id)sender
{
NSString* conversationFile = [@"~/" stringByAppendingPathComponent:@"conversation.txt"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:conversationFile];
if (fileExists == FALSE)
{
[self doShellScript:@"do shell script \"cd ~/; touch conversation.txt\""];
}
NSString *conversationContent = [[NSString alloc] stringWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
NSString *myMessage = [[messageBox stringValue]copy];
NSString *combinedContent = [NSString stringWithFormat:@"%@ \r\n %@", conversationContent, myMessage];
[[[myConversationBox textStorage] mutableString] setString: combinedContent];
[combinedContent writeToFile:@"~/conversation.txt" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
上面的代码出现以下错误
2011-07-07 21:38:08.703 iMessages[86493:903] -[NSPlaceholderString stringWithContentsOfFile:编码:错误:]: 无法识别的选择器发送到实例 0x100111690
2011-07-07 21:38:08.704 iMessages[86493:903] -[NSPlaceholderString stringWithContentsOfFile:编码:错误:]: 无法识别的选择器发送到实例 0x100111690
- (IBAction)sendMessage:(id)sender
{
NSString* conversationFile = [@"~/" stringByAppendingPathComponent:@"conversation.txt"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:conversationFile];
if (fileExists == FALSE)
{
[self doShellScript:@"do shell script \"cd ~/; touch conversation.txt\""];
}
NSString *conversationContent = [[NSString alloc] stringWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
NSString *myMessage = [[messageBox stringValue]copy];
NSString *combinedContent = [NSString stringWithFormat:@"%@ \r\n %@", conversationContent, myMessage];
[[[myConversationBox textStorage] mutableString] setString: combinedContent];
[combinedContent writeToFile:@"~/conversation.txt" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
The above code presents the following error
2011-07-07 21:38:08.703
iMessages[86493:903]
-[NSPlaceholderString stringWithContentsOfFile:encoding:error:]:
unrecognized selector sent to instance
0x1001116902011-07-07 21:38:08.704
iMessages[86493:903]
-[NSPlaceholderString stringWithContentsOfFile:encoding:error:]:
unrecognized selector sent to instance
0x100111690
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
stringWithContentsOfFile:encoding:error:
是NSString
的类方法,而不是实例方法,因此您不需要(不应该)先分配它。stringWithContentsOfFile:encoding:error:
is a class method ofNSString
, not an instance method, so you don't need to (shouldn't) alloc it first.使用
initWithContentsOfFile
代替stringWithContentsOfFile
或删除alloc
调用。所以有:或
Use
initWithContentsOfFile
in place ofstringWithContentsOfFile
or remove thealloc
call. So have:or