NSDocument读取RTFD数据

发布于 2024-08-22 08:36:04 字数 543 浏览 4 评论 0原文

我正在开发一个文本编辑器,我想添加 RTFD 支持。将 RTFD 文件类型添加到 Info.plist 后,我​​收到消息“readFromFileWrapper:ofType:error: 必须覆盖您的应用程序才能处理文件包”。

我浏览了文档并发现了一些东西,但我没能完成所有事情..有人可以这么好心帮助我吗?

- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError **)outError {

    if ([typeName isEqualToString:@"Rich Text with Attachments"])  {   
        NSLog(@"Its RTFD");
        //it gets to here but I don't know how to load the rtfd data then :(
}

ofType:typeName]    return YES; }

感谢任何帮助:)

I am working on a text editor and I want to add RTFD support. After adding the RTFD file type to my Info.plist, I got the message that "readFromFileWrapper:ofType:error: must be overridden for your application to handle file packages."

I had a look around in the documentation and found some things, but I didn't manage to do everything..Could someone be so kind and help me out please?

- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError **)outError {

    if ([typeName isEqualToString:@"Rich Text with Attachments"])  {   
        NSLog(@"Its RTFD");
        //it gets to here but I don't know how to load the rtfd data then :(
}

ofType:typeName]    return YES; }

Any help is apprechiated :)

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

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

发布评论

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

评论(2

浅听莫相离 2024-08-29 08:36:04

幸运的是,RTFD 是一种特别简单的方法。查看 NSTextreadRTFDFromFile:writeRTFDToFile:atomically: 方法。

来自 NSText 类参考< /a>:

从文件读取RTFD:

尝试读取路径处的 RTFD 文件,如果成功则返回 YES,如果失败则返回 NO

- (BOOL)readRTFDFromFile:(NSString *)路径

讨论
path 应该是 .rtf 文件或 .rtfd 文件包装器的路径,而不是 .rtfd 中的 RTF 文件的路径 文件包装器。

如果您来自 NSURL,请执行以下操作:

[textView readRTFDFromFile:[[self fileURL] path]];

为了安全起见,请先调用 [[self url] isFileURL]

RTFD is a particularly easy one, luckily. Check out the readRTFDFromFile: and writeRTFDToFile:atomically: methods of NSText.

From the NSText Class Reference:

readRTFDFromFile:

Attempts to read the RTFD file at path, returning YES if successful and NO if not.

- (BOOL)readRTFDFromFile:(NSString *)path

Discussion
path should be the path for an .rtf file or an .rtfd file wrapper, not for the RTF file within an .rtfd file wrapper.

If you're coming from NSURL, do something like this:

[textView readRTFDFromFile:[[self fileURL] path]];

To be safe, call [[self url] isFileURL] first.

世态炎凉 2024-08-29 08:36:04

我尝试了您的代码片段并实现了以下目标:

NSDocument 窗口已打开,标题已更新为 RTFD 文件的名称,但 NSTextView 仍为空白。

我非常确信我的代码会打开它,因为我从 NSDocument 实例获取了 URL,将其转换为 NSString 并将其发送到文本视图...

if ([typeName isEqualToString:@"Rich Text with Attachments"]) {
        NSString *urlString = [[self fileURL] absoluteString];
        NSLog(@"%@",urlString);
        [textView readRTFDFromFile:urlString];

        return YES;
    }

日志返回了以下内容:

file://localhost/Users/David/Documents/Coursework/Geography%20Coursework.rtfd/

那么它应该可以工作吗?

I tried your snippet and achieved the following:

The NSDocument window was opened, the title was updated to the RTFD file's name but the NSTextView is still blank.

I was pretty convinced that my code would open it, since I got myself the URL from the NSDocument instance, converted it to an NSString and sent it to the textview...

if ([typeName isEqualToString:@"Rich Text with Attachments"]) {
        NSString *urlString = [[self fileURL] absoluteString];
        NSLog(@"%@",urlString);
        [textView readRTFDFromFile:urlString];

        return YES;
    }

The Log returned this:

file://localhost/Users/David/Documents/Coursework/Geography%20Coursework.rtfd/

So it should work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文