NSUserDefaults: Дамп структуры standardUserDefaults NSUserDefaults

Это вызывает неопределенное поведение. От cppreference.com :

Поведение не определено, если указанная последовательность приращений или уменьшений потребует, чтобы итератор был без приращения (такой как итератор «за конец» ) или что невырожденный итератор (такой как передний итератор или сингулярный итератор) уменьшается.

blockquote>

17
задан Kevin Bomberry 30 April 2009 в 23:41
поделиться

5 ответов

Спасибо Дону МакКоги, моему деловому партнеру и другу, за то, что я исправил мой код и предоставил краткий ответ. Чтобы поделиться ею с остальными, вот фрагмент кода:

  NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
  NSString *bundleId = [bundleInfo objectForKey: @"CFBundleIdentifier"];

  NSUserDefaults *appUserDefaults = [[NSUserDefaults alloc] init];
  NSLog(@"Start dumping userDefaults for %@", bundleId);
  NSLog(@"userDefaults dump: %@", [appUserDefaults persistentDomainForName: bundleId]);
  NSLog(@"Finished dumping userDefaults for %@", bundleId);
  [appUserDefaults release];

Как видите, каждый, кто отвечал на вопрос, был на правильном пути, но ни один из предложенных кодов не был решением - до тех пор, пока Дон не отредактировал наш код в управления источником. Спасибо всем!

18
ответ дан 30 November 2019 в 10:01
поделиться

The shared NSUserDefaults is initialized with three search domains by default (you can add others too if you need to): app arguments, app preferences (what's stored in the app's plist), and localized system preferences. The last one is why you're seeing those unfamiliar Apple keys, but you don't really have to worry about "overwriting" them. If you use the same key name, it will just put that value in the app preferences domain. Your app's preferences is searched before the system preferences so you'll get the same value back, but it won't affect anything else.

If you really do want just your app's preferences though, you can remove the other search domains (the specific names you need is in the docs).

6
ответ дан 30 November 2019 в 10:01
поделиться
NSLog(@"NSUserDefaults dump: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
37
ответ дан 30 November 2019 в 10:01
поделиться

Try:

NSLog(@"NSUserDefaults dump: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

dictionaryRepresentation возвращает NSDictionary представление значений по умолчанию.

10
ответ дан 30 November 2019 в 10:01
поделиться
NSLog(@"%@ defaults = %@", [self class], 
  [[NSUserDefaults standardUserDefaults] 
   persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);
8
ответ дан 30 November 2019 в 10:01
поделиться