【问题标题】:saving the contents of NSArray as plain text file and not at xml将 NSArray 的内容保存为纯文本文件而不是 xml
【发布时间】:2014-08-15 19:03:01
【问题描述】:

我正在尝试将 NSArray 的内容保存为纯文本文件。这是我如何保存数组的内容:

[myArrayContent writeToFile:filePath atomically:YES];

但是被保存为xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>lineOne</string>
    <string>lineTwo</string>
</array>
</plist>

我对你们的问题是如何将内容保存为纯文本:

lineOne 第二行

你们中的任何人都知道如何实现这个吗?

非常感谢您的帮助。

【问题讨论】:

    标签: ios objective-c xcode5 nsarray


    【解决方案1】:

    你可以把一个数组变成一个字符串,然后保存,像这样:

    NSString *fileContent = [myArrayContent componentsJoinedByString: @"\n"];
    [fileContent writeToFile:filePath atomically:YES];
    

    回读是这样完成的:

    NSArray *myArrayContent = [
        [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]
        componentsSeparatedByString:@"\n"
    ];
    

    【讨论】:

    • 已弃用,现在您将使用类似 [fileContent writeToFile:[self filepath] atomically:YES encoding:NSUTF8StringEncoding error:&err];
    【解决方案2】:

    鉴于您的数组中的对象是 NSString,您应该遍历数组中的对象并在每次迭代中写入字符串,就像您处理数组一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      相关资源
      最近更新 更多