Question

Today I created a project with local data storage (plist). Everything worked fine until I renamed my plist-file (from: Log.plist to: Data.plist). I renamed it inside Xcode, so there's no wrong connection.

What could it be? I already cleared Xcode, removed all files in DerivedData, but noting works. The data won't get saved in my plist-file.

Also tried to recreate the project with another name or restore the files from Time Capsule: Every time I rename the plist, nothing gets stored anymore.

Does anyone have an idea why no data gets stored? Thanks for your time.

EDIT

    - (void)viewDidLoad
{
    [super viewDidLoad];

    [self retrieveDataFromPlist:@"Data"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger results = tableViewData.count;

    return results;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"logDataCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    [cell.textLabel setText:[tableViewData objectAtIndex:indexPath.row]];

    return cell;
}

- (void)retrieveDataFromPlist:(NSString *)plist
{
    NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", plist]];

    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:plist ofType:@"plist"];
    }

    NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSString *plistError;
    NSPropertyListFormat plistFormat;
    NSDictionary *plistDictionary = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&plistFormat errorDescription:&plistError];

    if (!plistDictionary) {
        NSLog(@"Error retrieving data from plist: %@", plistError);
    }

    tableViewData = [plistDictionary objectForKey:@"Logs"];

    [self.logTableView reloadData];
}

- (void)storeData:(NSString *)data inPlist:(NSString *)plist
{
    NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", plist]];

    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:plist ofType:@"plist"];
    }

    NSMutableArray *refreshedTableData = [NSMutableArray arrayWithCapacity:25];

    for (int i = 0; i < tableViewData.count; i++) {
        [refreshedTableData addObject:[tableViewData objectAtIndex:i]];
    }

    [refreshedTableData insertObject:data atIndex:0];

    // remove last object after 25 logs to keep storage as small as possible
    if (refreshedTableData.count > 25) {
        [refreshedTableData removeLastObject];
    }

    NSDictionary *plistDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:refreshedTableData, nil] forKeys:[NSArray arrayWithObjects:@"Logs", nil]];
    NSString *plistError;
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&plistError];

    if (plistData) {
        [plistData writeToFile:plistPath atomically:YES];
    } else {
        NSLog(@"Error storing data in plist: %@", plistError);
    }

    // retrieve refreshed data so tableView is always filled with the latest logs
    [self retrieveDataFromPlist:@"Data"];
}

- (void)deleteFullPlist:(NSString *)plist
{

}

- (IBAction)menuButtonClicked:(id)sender
{
    [self.slidingViewController slideRight];
}

- (IBAction)saveButtonClicked:(id)sender
{
    [self storeData:self.textfield.text inPlist:@"Data"];
}
Was it helpful?

Solution

It is not possible to save the plist to the app's bundle.

- (void)storeData:(NSString *)data inPlist:(NSString *)plist {
    ...
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:plist ofType:@"plist"];
    }

Remove the last 3 lines above, you always need to store in the Document directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top