0

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

Ramanan R R
  • 896
  • 8
  • 18

2 Answers2

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

  }
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • NSData *fileData = documentData; //file received from iCloud ** now i convert //NSData to str ** NSString* myString; myString = [[NSString alloc] initWithData:fileData encoding:NSASCIIStringEncoding]; **While NSLog this "myString" it shows correctly. But issue happens here NSMutableArray *arrayPlist = [NSMutableArray arrayWithContentsOfFile:myString]; NSLog -> arrayPlist shows null. – Ramanan R R Nov 01 '14 at 08:12
  • second code in question starts myString shows in NSLog (plist stuff converted data to string)! – Ramanan R R Nov 01 '14 at 08:23
  • K.Did u create Plist(New File->IOS-Resource->Property List) for those setting keys? – user3182143 Nov 01 '14 at 08:27
  • I will tell you clearly now.Above my code is for if you have set the key in Plist you can simply use that my above code.That is in Bundle.That is the reason why we have to mention the [[NSBundle mainBundle]pathForResource:plistName ofType:@"plist"].After that we can get those values from Plist to array. – user3182143 Nov 01 '14 at 08:32
0

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
}
Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Is there any possible way to turn this plist data to array like I posted in "Printing description of arrTemp:" ? – Ramanan R R Nov 01 '14 at 07:08
  • It **is** an array. You posted an array with a single element, which is a dictioanry — **both times**. The two representations show the same data structure. You are confused if you think they are different. – Ken Thomases Nov 01 '14 at 07:32