Getting plist data in a order

此生再无相见时 提交于 2019-12-13 06:59:20

问题


I save my datas in plist. Once my app is deleted and reinstalled I planned to get those plist datas through iCloud. I have upload option to send file to cloud and retrieve when app installed new. Once app is deleted/reinstalled the plist data is retrieved with no issue. Now the problem is I save my files like this in a array and write to 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;
}
)

When I retrieve from iCloud the plist looks like this:

<?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>

I cannot write this data retrieved from cloud straight to my plist when app is installed new. I have to turn back the plist datas to like the array (posted initially "Printing description of arrTemp:") and write it to my path when my app installed new. How to turn the data from plist to new array should look like I posted initially in "Printing description of arrTemp:" ?? Please Help


回答1:


STEP 1:Create NSObject Class name as 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;

}

STEP 2:In your view controller

   In viewDidLoad Method

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

STEP 3:Fetching Valus From array

  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"]];

  }



回答2:


Your plist is an array with a single element, which is a dictionary. Dictionaries are unordered collections. An array is ordered and the order is being preserved.

Also, you seem to be confused about the format used to log an array, which is the result of calling -description on the array, and the format of the plist. NSArray and NSDictionary describe themselves using an old format, but that has nothing to do with how they are actually stored on disk or in data streams when a property list is saved or serialized. Do not compare the two forms and expect them to be the same.


Is the problem that you're retrieving the plist as just data and you don't know how to deserialize that to an NSArray object?

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
}


来源:https://stackoverflow.com/questions/26686818/getting-plist-data-in-a-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!