开发者

Save contents of NSTextView as a plaintext file

I'm sure this has been asked and answered before, but I'm struggling to find something that does what I need. I'm also new to Cocoa development.

Essentially I need to take the contents of an NSTextView and save it as a plaintext (.txt) file. 开发者_运维知识库 I'm assuming some sort of formatting has to to occur before writing to file, etc...


You can get the data from the NSTextView by using the string method; you can then take that string and write it directly to a file with the writeToFile:atomically:encoding:error: method of NSString. A quick example for stdout-kind of data:

[[myTextView string] writeToFile:@"~/example.txt" 
                      atomically:YES 
                        encoding:NSASCIIStringEncoding 
                           error:nil];

But you probably should handle errors if you're going to use this code in a more complicated situation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜