我正在尝试在NSUserDefaults
中设置密钥。控制台日志显示密钥auto_reset_switch
设置为" YES"当我注册默认值。但是,当我稍后读取默认值时,auto_reset_switch键为" 0"。我尝试将密钥设置为BOOL
或NSString
,但没有区别。我正在按照书中的文字" iOS编程Big Nerd Ranch Guide第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&#34;
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]);
但现在控制台日志显示&#34; auto_reset_switch = 0&#34;:
NSPersonNameDefaultDisplayNameOrder = 2;
PKEnableStockholmSettings = 1;
"auto_reset_switch" = 0;
答案 0 :(得分:0)
假设您希望此值为BOOL
而非字符串值,请将@"YES"
更改为@YES
。这将使其成为BOOL
(包裹在NSNumber
而不是NSString
。
NSDictionary *factorySettings = @{NRCAutoResetSwitch : @YES};
答案 1 :(得分:0)
我在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];
}