【问题标题】:Enter text in the app folder [closed]在应用程序文件夹中输入文本[关闭]
【发布时间】:2014-03-26 14:02:17
【问题描述】:
- (void)writeStringToFile:(NSString*)myString {

NSURL *myURL = [[NSBundle mainBundle]URLForResource:@"nombre" withExtension:@"txt"];
NSString *myFileViaURL = [[NSString alloc]initWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:nil];
[myString writeToFile:myFileViaURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
 }

也可以在拖到xcode的txt文件中插入文本。

【问题讨论】:

  • 问题是什么?问题是什么?这是什么我什至没有?

标签: ios text


【解决方案1】:

如果您尝试更改应用程序包中的某些文件 - 这是不可能的。 如果您想在设备上存储一些数据并能够更改它 - 您需要将数据保存在其他地方。 你的猫尝试使用这样的东西:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  _path = [documentsDirectory stringByAppendingPathComponent:@"nombre.txt"];

  NSFileManager *fileManager = [NSFileManager defaultManager];

  if(![fileManager fileExistsAtPath:_path]) {
    NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"nombre.txt"];
    success = [fileManager copyItemAtPath:defaultPath toPath:_path error:&error];
    if (success) {
      [myString writeToFile:_path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多