Objective-C 文件 I/O 错误

发布于 2024-11-18 22:01:32 字数 1217 浏览 4 评论 0原文

- (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
0x100111690

2011-07-07 21:38:08.704
iMessages[86493:903]
-[NSPlaceholderString stringWithContentsOfFile:encoding:error:]:
unrecognized selector sent to instance
0x100111690

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-11-25 22:01:32

stringWithContentsOfFile:encoding:error:NSString 的类方法,而不是实例方法,因此您不需要(不应该)先分配它。

NSString *conversationContent = [NSString stringWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];

stringWithContentsOfFile:encoding:error: is a class method of NSString, not an instance method, so you don't need to (shouldn't) alloc it first.

NSString *conversationContent = [NSString stringWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
伊面 2024-11-25 22:01:32

使用 initWithContentsOfFile 代替 stringWithContentsOfFile 或删除 alloc 调用。所以有:

NSString *conversationContent = [[NSString alloc] initWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];

NSString *conversationContent = [NSString stringWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];

Use initWithContentsOfFile in place of stringWithContentsOfFile or remove the alloc call. So have:

NSString *conversationContent = [[NSString alloc] initWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];

or

NSString *conversationContent = [NSString stringWithContentsOfFile:@"~/conversation.txt" encoding:NSUTF8StringEncoding error:NULL];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文