用xcode中的现有数据编写plist

时间:2014-10-31 07:22:42

标签: ios objective-c xcode plist

我可以选择在我的应用中将plist文件上传到iCloud。当应用程序被删除并安装回来时,内容必须保留。这是我一直在尝试的,我没有从iCloud上传/检索任何问题。我从云端获得了作为NSData的plist细节。现在的问题是我已将这些数据写回我的plist已存在。

NSArray *paths           = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);

NSString *documentsDir   = [paths objectAtIndex:0];
NSString *root          = [documentsDir stringByAppendingPathComponent:@"Receipts.plist"];

我从云中获取了作为NSData的plist内容,在转换为NSString后,这就是我得到的。

<?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>
    <dict>
        <key>Amount</key>
        <string>USD 1500.00</string>
        <key>Category</key>
        <string></string>
        <key>Currency</key>
        <string>USD</string>
        <key>Date</key>
        <string>Thu 30 Oct,2014</string>
        <key>DateSort</key>
        <string>2014/10/30</string>
        <key>Merchant</key>
        <string></string>
        <key>Payment</key>
        <string></string>
        <key>Purpose</key>
        <string>-</string>
        <key>SubCategory</key>
        <string></string>
        <key>TimeStamp</key>
        <string>Thu 30 Oct,2014 14:42:47</string>
        <key>Tips</key>
        <string>0.00</string>
        <key>UDDate1</key>
        <string>Thu 30 Oct,2014</string>
        <key>UDDate2</key>
        <string>Thu 30 Oct,2014</string>
        <key>UDNumber1</key>
        <string>0.00</string>
        <key>UDNumber2</key>
        <string>0.00</string>
        <key>UDNumber3</key>
        <string>0.00</string>
        <key>UDText1</key>
        <string>-</string>
        <key>UDText2</key>
        <string>-</string>
        <key>UDText3</key>
        <string>-</string>
        <key>Uploaded</key>
        <string>upload</string>
        <key>Value</key>
        <string>0</string>
    </dict>
</array>
</plist>

我在写回旧内容时发现问题。如何将这些内容写回这个空的plist?

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助。你的plist是一个数组,所以将数组保存到文件中。

//read from file
NSArray *array = [NSArray arrayWithContentsOfFile:root];
//wirte to file
NSArray *sourceArray = [NSArray arrayWithContentsOfFile:filePath];
BOOL success = [sourceArray writeToFile:root atomically:YES];