Objective-c 保存原始文本

发布于 2024-08-16 06:28:58 字数 318 浏览 1 评论 0原文

我在基于文档的应用程序中实现了保存和加载方法。在保存方法中,我有

[NSArchiver archivedDataWithRootObject:[self string]];

Where [self string] is a NSString。 当保存仅包含“正常内容”的文件时,创建的文件的内容为:

streamtypedè@NSStringNSObject+普通内容

有没有办法在文件中仅存储原始文本?

感谢您的帮助。

I implemented saving and loading methods in my document-based application. In the saving method, I have

[NSArchiver archivedDataWithRootObject:[self string]];

Where [self string] is a NSString.
When saving a file with just "normal content" inside of it, the contents of the file created are:

streamtypedè@NSStringNSObject+normal content

Is there a way to store in a file just raw text?

Thanks for your help.

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

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

发布评论

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

评论(2

痴骨ら 2024-08-23 06:28:58

NSString 内部有一些方法用于保存在文件中:

NSString * s = @"Foo bar";

NSError * err = NULL;
BOOL result = [s writeToFile:@"/tmp/test.txt" atomically:YES encoding:NSASCIIStringEncoding  error:&err];

There are methods inside NSString for saving in a file:

NSString * s = @"Foo bar";

NSError * err = NULL;
BOOL result = [s writeToFile:@"/tmp/test.txt" atomically:YES encoding:NSASCIIStringEncoding  error:&err];
秉烛思 2024-08-23 06:28:58

由于我是可可新手,我不知道这是否是正确的方法,甚至是有效的方法。

但快速查看文档后,我发现了 NSString 实例的这种方法, - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

快速尝试了一个示例项目,它运行良好:
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError

所以这样的东西可能适合你:

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
    return [[self string] dataUsingEncoding:NSUnicodeStringEncoding];
}

Since i am new with cocoa, i don't know if this is the right way to do it or even a valid way.

But after a quick look at the documentation i found this method of NSString instances, - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

A quick try on a sample project it worked fine with:
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError

So something like this might work for you:

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
    return [[self string] dataUsingEncoding:NSUnicodeStringEncoding];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文