目标C:以编程方式将文件写入主包 [英] Objective C: Write a file programmatically to the Main Bundle

查看:92
本文介绍了目标C:以编程方式将文件写入主包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

写入文件目标c

如何我创建和写一个文件到捆绑?我有这个:

How do I create and write a file to the bundle? I have this:

//make path to file
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"usnews.txt", [sharedManager.articleNumber intValue] + 1]];
//write file
[textContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

,但会将其写入文档文件夹,并在应用关闭时删除。

but it writes it to the documents folder and is deleted when the app is closed.

推荐答案

您无法写入iOS上的主包。其内容作为App Store提交过程的一部分进行加密签名;修改其内容将阻止应用程序运行。

You can’t write to the main bundle on iOS. Its contents are cryptographically signed as a part of the App Store submission process; modifying its contents would prevent the application from running.

应用程序关闭时不应删除文档。尝试传递 NSError 以接收写入时的错误消息。此外,您确定该文件正在写入您认为的位置吗?您使用此文件名:

Documents shouldn’t be deleted when the app is closed. Try passing in an NSError to receive error messages on your write. Also, are you sure that the file is writing to the location you think? You use this for the filename:

[NSString stringWithFormat:@"usnews.txt", [sharedManager.articleNumber intValue] + 1]

但您没有%d 格式说明符,因此数字不会插入到文件名中。

But you don’t have a %d format specifier in it, so the number is never inserted into the filename.

这篇关于目标C:以编程方式将文件写入主包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆