Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 将NSTextViewData保存到RTF文档_Cocoa_Rtf_Nssavepanel - Fatal编程技术网

Cocoa 将NSTextViewData保存到RTF文档

Cocoa 将NSTextViewData保存到RTF文档,cocoa,rtf,nssavepanel,Cocoa,Rtf,Nssavepanel,正在尝试将NSTextView数据导出到RTF文档。我的旧代码(主要是NSSavePanel中的文件名)已贬值。文档状态使用URL。我该怎么做 谢谢 NSSavePanel *panel = [NSSavePanel savePanel]; [panel setAllowedFileTypes:[NSArray arrayWithObject:@"rtf"]]; if ([panel runModal] == NSOKButton){ [[textView RTFFromRange:

正在尝试将NSTextView数据导出到RTF文档。我的旧代码(主要是NSSavePanel中的文件名)已贬值。文档状态使用URL。我该怎么做

谢谢

NSSavePanel *panel = [NSSavePanel savePanel];

[panel setAllowedFileTypes:[NSArray arrayWithObject:@"rtf"]];
if ([panel runModal] == NSOKButton){


[[textView RTFFromRange:
      NSMakeRange(0, [[textView string] length])] 
     writeToFile:[panel filename] atomically:YES];

}

正如文档所说,您应该使用NSSavePanel的URL方法

代码看起来相同,但您将使用NSString writeToURL:Atomicaly:encoding:error:method代替:

NSSavePanel *panel = [NSSavePanel savePanel];

[panel setAllowedFileTypes:[NSArray arrayWithObject:@"rtf"]];
if ([panel runModal] == NSOKButton){
    [[textView RTFFromRange:NSMakeRange(0, [[textView string] length])] writeToURL:[panel URL] atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
注意这里我设置的两个参数来指定一个编码UTF-8和一个错误对象。我在这里给出NULL,但您可能会给出一个有效的对象来获取错误信息