Trying to export NSTextView data to an RTF doc. My old code, mainly "filename" from NSSavePanel is depreciated. The docs state "use URL". How can I do this?

Thanks.

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];

}
有帮助吗?

解决方案

As the doc says, you should use the URL method of NSSavePanel.

The code will look the same, but you'll use the NSString writeToURL:atomically:encoding:error: method instead :

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];
}

Note the two parameters to specify an encoding (here I set UTF-8), and an error object. I give NULL here, but you would perhaps give a valid object to get error information.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top