views:

146

answers:

2

I have an app that needs to connect and receive data, different each time that you click in one tab. Then to show the data to the user, i use a "element.plist" where i have one array of dictionaries( each dictionary has the info in different strings: name, category, ...). I load the info from this plist.

I would like then, to continue using the same structure. Each time i receive the connection data:

  1. delete the content in the plist
  2. save the new content (I can do this in the parser method, each time that i have one object with all the information)
  3. Read the info like i'm doing now.

The step that i can't do is the second.

thanks

A: 

I'm not sure I completely understand your question, but I'll try to help.

below is some apple sample code that saves a plist when an application is exiting.

the second line sets the name of the plist file:

NSString *bundlePath = the application directory + "Data"

the third line defines a dictionary with all the data to be saves:

NSDictionary *plistDict

the fourth line formats this dictionary as property list data:

NSData *plistData

which then gets saved as Data.plist

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender 
{
    NSString *errorDesc;
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
            [NSArray arrayWithObjects: personName, phoneNumbers, nil]
            forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
            format:NSPropertyListXMLFormat_v1_0
            errorDescription:&errorDesc];
    if (plistData) 
    {
        [plistData writeToFile:bundlePath atomically:YES];
    }
    else {
        NSLog(errorDesc);
        [errorDesc release];
    }
    return NSTerminateNow;
}

You can find this information in the Property list programming guide

compound eye
I create a plist type Dictionary (also try with type array) named Data.plist.I executed this code (adding the personName and phoneNumbers objects) and my plist continue empty.
this code pattern has worked for me.perhaps you could start by trying predefined strings in the values array: NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: @"aName", @"aNumber", nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]]; NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:to ensure the problem is no with the values you are passing in?
compound eye
A: 

Mey,

I'm not sure that I understand your statement about having an empty plist. I assume that you mean that if you read back the plist file that you created, it is null when you print it out. Suggesting that you are writing out an empty file or not reading correct or ...

I further assume that your intent is to replace the existing plist contents by a new plist while keeping the same name.

And caveat emptor - I'm new to Objective C etc. Here is a way to do that which I think you are trying to do.

// Implement viewDidLoad to do additional setup after loading the view, 
// typically from a nib.
- (void)viewDidLoad {
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"TmpPList" ofType:@"plist"]; //Not NARC
    //NSLog(@"plistPath : %@", plistPath);
    //My plist is a simple array, but it could be an array of dictionary objects etc
    NSMutableArray *arrayFromPList = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; //NARC
    //NSLog(@"arrayFromPList : %@", arrayFromPList);
    //Delete the arrays contents and put new contents
    [arrayFromPList removeAllObjects];
    //NSLog(@"arrayFromPList : %@", arrayFromPList);
    //[arrayFromPList addObjectsFromArray:[NSArray arrayWithObjects:@"A", @"B", "@C", nil]];
    //NSLog(@"arrayFromPList : %@", arrayFromPList);
    [arrayFromPList setArray:[NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", nil]];
    //NSLog(@"arrayFromPList : %@", arrayFromPList);
    /* */
    //Write it out to the original file name
    [arrayFromPList writeToFile:plistPath atomically:YES];
    NSMutableArray *newArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; //NARC
    NSLog(@"newArray : %@", newArray);
    [arrayFromPList release];
    [newArray release];    
}