0

Occasionally, especially if I have played with settings or the app has been force quit all the formatting is messed up because these values come back null.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        bool bold = [[NSUserDefaults standardUserDefaults] boolForKey:@"bold"];    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 0;
        }

        int fontSize = [[NSUserDefaults standardUserDefaults] integerForKey:@"fontSize"];
        NSString *fontFace = bold? @"Helvetica":[[NSUserDefaults standardUserDefaults] stringForKey:@"fontFace"];
        cell.textLabel.font = [UIFont fontWithName:fontFace size:(float)fontSize];

        NSString *cellText = romanized? @"Romanized":@"Text";

        cell.textLabel.text = [[self.myDataArray objectAtIndex:indexPath.row] objectForKey:cellText];

        return cell;
    }

Edit with code from InAppSettingsKit, in my rootview controller I do this:

    - (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController*)sender {
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self dismissModalViewControllerAnimated:YES];
        // your code here to reconfigure the app for changed settings
    }
Alienz
  • 118
  • 2
  • 9
  • 2
    When you set values in NSUserDefaults, do you then call `synchronize` on the defaults object? – Mark Granoff Oct 07 '11 at 18:16
  • I am using InAppSettingsKit. I do synchronize when it returns back to my rootviewcontroller. – Alienz Oct 07 '11 at 18:18
  • I've not used that library, so I can't comment definitively. `synchronize` was my best suggestion. :-) Perhaps something is getting destroyed before it can be written out? The problem may be completely unrelated, of course, to NSUserDefaults. Could just be that your app is being killed by the IDE (if you're running in the debugger) or crashing, either case being a pretty hard stop for you app leaving no chance of persisting any data. – Mark Granoff Oct 07 '11 at 18:25
  • Thanks, I was mostly able to recreate it by doing crashes. I added sync to applicationwillterminate and now it seems to be keeping values much better. – Alienz Oct 07 '11 at 18:30
  • @MarkGranoff btw its a pretty awesome library. It basically brings your settings from settings.app into your app. You can call it in all sorts of ways. – Alienz Oct 07 '11 at 19:15

1 Answers1

1

When you force quit the app, the user defaults are not saved.

My suggestion would be to check for every key at launch, if it exists, do nothing, if it doesn't, set a default value to it.

EDIT: This is how I'd do it for many objects:

NSArray *keys = [NSArray arrayWithObjects:@"Key 1", @"Key 2", @"Key 3", nil];
NSArray *defaultValues = [NSArray arrayWithObjects:@"Default Value", [UIFont systemFontOfSize:12], [NSNumber numberWithInteger:5], nil];
for(NSInteger i = 0; i < [keys count]; i++){
  id object = [[NSUserDefaults standardUserDefaults] objectForKey:[keys objectAtIndex:i];
  if(!object)[[NSUserDefaults standardUserDefaults] setObject:[defaultValues objectAtIndex:i] forKey:[keys objectAtIndex:i]];
}
EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • Thanks, I have about 15 settings now and we are going to be adding more. Is there any function out there that does this easily without having to manually specify? – Alienz Oct 07 '11 at 18:29
  • I edited the answer with a little of code of how I'd do it for many settings, just add a new key and a new default value. – EmilioPelaez Oct 07 '11 at 18:34
  • is there a way to pull from root.plist? That would be better right? – Alienz Oct 07 '11 at 18:37
  • Yeah, just load a dictionary from the plist and read the 2 arrays. However, remember that not all objects can be stored in a plist. – EmilioPelaez Oct 07 '11 at 18:42
  • Thanks, I found this: http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the-s – Alienz Oct 07 '11 at 19:00