获取订单中的plist数据

时间:2014-11-01 06:36:13

标签: ios objective-c xcode plist

我将数据保存在plist中。删除并重新安装我的应用程序后,我计划通过iCloud获取这些plist数据。我有上传选项将文件发送到云,并在应用程序安装新的时检索。删除/重新安装应用程序后,将检索plist数据,没有任何问题。现在问题是我在数组中保存我的文件并写入plist:

Printing description of arrTemp:
<__NSCFArray 0x175f7d90>(
{
    Amount = "USD 100.00";
    Category = "";
    Currency = USD;
    Date = "Fri 31 Oct,2014";
    DateSort = "2014/10/31";
    Merchant = "";
    Payment = "";
    Purpose = "-";
    SubCategory = "";
    TimeStamp = "Fri 31 Oct,2014 13:35:07";
    Tips = "0.00";
    UDDate1 = "Fri 31 Oct,2014";
    UDDate2 = "Fri 31 Oct,2014";
    UDNumber1 = "0.00";
    UDNumber2 = "0.00";
    UDNumber3 = "0.00";
    UDText1 = "-";
    UDText2 = "-";
    UDText3 = "-";
    Uploaded = upload;
    Value = 0;
}
)

当我从iCloud检索时,plist看起来像这样:

<?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 100.00</string>
        <key>Category</key>
        <string></string>
        <key>Currency</key>
        <string>USD</string>
        <key>Date</key>
        <string>Thu 31 Oct,2014</string>
        <key>DateSort</key>
        <string>2014/10/31</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>Fri 31 Oct,2014 13:35:07</string>
        <key>Tips</key>
        <string>0.00</string>
        <key>UDDate1</key>
        <string>Fri 31 Oct,2014</string>
        <key>UDDate2</key>
        <string>Fri 31 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。我必须将plist数据转回到类似数组(最初发布的“arrTemp的打印描述:”),并在我的应用程序安装新的时将其写入我的路径。如何将数据从plist转换为新数组应该看起来像我最初在“打印描述arrTemp:”中发布的?请帮忙

2 个答案:

答案 0 :(得分:1)

步骤1:将NSObject类名称创建为PList。

in .h

#import <Foundation/Foundation.h>

@interface PList : NSObject

+(NSMutableArray *)arrayPlistInit:(NSString *)plistName;

@end

in .m

+(NSMutableArray *)arrayPlistInit:(NSString *)plistName

{

  NSString *stringPath = [[NSBundle mainBundle]pathForResource:plistName ofType:@"plist"];

  NSMutableArray *arrayPlist = [NSMutableArray arrayWithContentsOfFile:stringPath];

  return arrayPlist;

}

第2步:在视图控制器中

   In viewDidLoad Method

     arrTemp = [PList arrayPlistInit:PUBLIC]; //I think PUBLIC is the plist name.

第3步:从数组中获取Valus

  In viewDidLoad Method after arrTemp

   for(int i=0;i<[arrTemp count];i++)

   {

     NSString *strAmount = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0]valueForKey:@"Amount"]];

     NSString *strCategory = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"Category"]];

     NSString *strCurrency = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0]valueForKey:@"Currency"]];

     NSString *strDate= [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"Date"]];

     NSString *strDateSort = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"DateSort"]];

     NSString *strMerchant = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0]valueForKey:@"Merchant"]]; 

     NSString *strPayment = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"Payment"]];

     NSString *strPurpose = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"Purpose"]];

     NSString *strSubCategory = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"SubCategory"]];

     NSString *strTimeStamp= [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0]valueForKey:@"TimeStamp"]];

     NSString *strTips = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"Tips"]];

     NSString *strUDDate1 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"UDDate1"]]; 

     NSString *strUDDate2 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"UDDate2"]];

     NSString *strUDNumber1 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0]valueForKey:@"UDNumber1"]]; 

     NSString *strUDNumber2 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"UDNumber2"]];

     NSString *strUDNumber3 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"UDNumber3"]]; 

     NSString *strUDText1 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"UDText1"]]; 

     NSString *strUDText2 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"UDText2"]]; 

     NSString *strUDText3 = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"UDText3"]]; 

     NSString *strUploaded = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"Uploaded"]]; 

     NSString *strValue = [NSString stringWithFormat:@"%@",[[arrTemp objectAtIndex:0] valueForKey:@"Value"]];

  }

答案 1 :(得分:0)

你的plist是一个包含单个元素的数组,它是一个字典。字典是无序集合。订购数组并保留订单。

此外,您似乎对用于记录数组的格式感到困惑,这是在数组上调用-description的结果,以及plist的格式。 NSArrayNSDictionary使用旧格式描述自己,但这与在保存或序列化属性列表时它们实际存储在磁盘或数据流中的方式无关。不要比较这两种形式,并期望它们是相同的。


问题是您是否只是将plist检索为数据而您不知道如何将其反序列化为NSArray对象?

NSData* data = /* data object containing the plist XML as you posted in your question */;
NSError* error;
NSArray* array = [NSPropertyListSerialization propertyListWithData:data
                                                           options:NSPropertyListImmutable
                                                            format:NULL
                                                             error:&error];
if (!array)
{
    NSLog(@"failed to deserialize plist data %@: %@", data, error);
    // additional error handling
}
if (![array isKindOfClass:[NSArray class]])
{
    NSLog(@"plist data is not an array as expected; data %@; got %@", data, array);
    // additional handling for invalid data
}