我正在尝试在NSUserDefaults
中设置一个键。控制台日志显示当我注册默认值时设置为"YES"的密钥auto_reset_switch
。但是,当我稍后读取默认值时,auto_reset_switch键为"0"。我试过将密钥设置为BOOL
或NSString
,但没有什么区别。我正在阅读《iOS编程大Nerd牧场指南第4版》一书中的文字。我做错了什么?
这是appDelegate.h:
//
// NRCAppDelegate.h
// LifeWatch
//
// Created by Nelson Capes on 10/13/2015.
// Copyright © 2015 Nelson Capes. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "NRCAlertView.h"
extern NSString * const NRCAutoResetSwitch;
@interface NRCAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
这是在appDelegate.m:中设置的密钥
//
// NRCAppDelegate.m
// LifeWatch
//
// Created by Nelson Capes on 10/13/2015.
// Copyright © 2015 Nelson Capes. All rights reserved.
//
#import "NRCAppDelegate.h"
#import "NRCItemsViewController.h"
#import "NRCItemStore.h"
#import "NRCImageStore.h"
#import "NRCAppDelegate.h"
NSString * const NRCAutoResetSwitch = @"auto_reset_switch";
@class NRCItemsViewController;
@implementation NRCAppDelegate
+(void)initialize
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"defaults %@", [defaults
dictionaryRepresentation]);
NSDictionary *factorySettings = @{NRCAutoResetSwitch : @"YES"};
[defaults registerDefaults:factorySettings];
NSLog(@"defaults %@", [defaults
dictionaryRepresentation]);
}
这是注册默认值后的控制台日志(其中的一部分)。注意**auto_reset_switch=YES"
2016-01-12 17:23:16.088 LifeWatch[373:20370] defaults {
NSPersonNameDefaultDisplayNameOrder = 2;
PKEnableStockholmSettings = 1;
"auto_reset_switch" = YES;
"com.apple.content-rating.AppRating" = 1000;
"com.apple.content-rating.ExplicitBooksAllowed" = 1;
"com.apple.content-rating.ExplicitMusicPodcastsAllowed" = 1;
"com.apple.content-rating.MovieRating" = 1000;
"com.apple.content-rating.TVShowRating" = 1000;
}
下面是读取默认值的代码:
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
NSLog(@"defaults %@", [defaults
dictionaryRepresentation]);
但现在控制台日志显示"auto_reset_switch=0":
NSPersonNameDefaultDisplayNameOrder = 2;
PKEnableStockholmSettings = 1;
"auto_reset_switch" = 0;
假设您希望它是BOOL
值而不是字符串值,请将@"YES"
更改为@YES
。这将使其成为BOOL
(包装在NSNumber
而不是NSString
中
NSDictionary *factorySettings = @{NRCAutoResetSwitch : @YES};
我在stackoverflow上找到了这个答案(我不知道如何链接到它,所以我不想把这个答案归功于它。代码运行良好。
我认为OP中的示例是错误的代码。
- (void)registerDefaultsFromSettingsBundle
{
NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
if(!settingsBundle)
{
//NSLog(@"Could not find Settings.bundle");
return;
}
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
for(NSDictionary *prefSpecification in preferences)
{
NSString *key = [prefSpecification objectForKey:@"Key"];
if(key)
{
[defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
}
}
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
}