{"id":13651,"date":"2016-02-22T11:39:58","date_gmt":"2016-02-22T16:39:58","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=13651"},"modified":"2025-12-16T15:49:44","modified_gmt":"2025-12-16T20:49:44","slug":"use-and-misuse-of-nsuserdefaults","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/02\/22\/use-and-misuse-of-nsuserdefaults\/","title":{"rendered":"Use and Misuse of NSUserDefaults"},"content":{"rendered":"<p><a href=\"http:\/\/dscoder.com\/defaults.html\">David Smith<\/a> (<a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/700202881027170304\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/dscoder.com\/defaults.html\"><p>If you find yourself needing to do anything else to read a preference, you should take a step back and reconsider: caching values from NSUserDefaults is usually unnecessary, since it&rsquo;s extremely fast to read from. Calling <code>-synchronize<\/code> before reading a value is always unnecessary. Responding when the value changes is almost always unnecessary, since the nature of &ldquo;settings&rdquo; is that they control what a program does when it does it, rather than actually causing it to do something. Having an alternate code path for &ldquo;no value set&rdquo; is also generally unnecessary, as you can provide a default value instead (see Providing Default Values below).<\/p><p>[&#8230;]<\/p><p>You can call <code>-registerDefaults:<\/code> as many times as you like, and it will combine the dictionaries that you pass it, which means you can keep registration of settings near the code that cares about them.<\/p><\/blockquote><p>I used to use <code>-registerDefaults:<\/code> more, but now I mostly use a category method like:<\/p>\n<pre>- (id)mjtObjectForKey:(NSString *)key defaultValue:(id)defaultValue;<\/pre>\n<p>that returns <code>defaultValue<\/code> if <code>NSUserDefaults<\/code> returns <code>nil<\/code>. This is more convenient (one line of code) and makes it impossible to look up a key that has not been registered.<\/p>\n<blockquote cite=\"http:\/\/dscoder.com\/defaults.html\"><p>In the sandboxed world of modern OSX and all iOS versions, NSUserDefaults is initially limited to operating in your app&rsquo;s sandbox; if you use <code>-initWithSuiteName:<\/code> you&rsquo;ll just get a new store of user defaults that&rsquo;s still not shared.<\/p><p>[&#8230;]<\/p><p>NSUserDefaults does not have any form of transaction system, so there&rsquo;s no way to guarantee that multiple changes will only be seen all at once. Another program could see the first change before the second finishes.<\/p><p>[&#8230;]<\/p><p>Neither <code>NSUserDefaultsDidChangeNotification<\/code> nor KVO notify of changes made by other programs<\/p><p><code>-registerDefaults:<\/code> operates on every <code>NSUserDefaults<\/code> instance, not just the one you call it on<\/p><\/blockquote>\n<p>Update (2016-02-22): One of the issues&nbsp;I&rsquo;ve found is that, starting with Mac OS X 10.10, <code>NSUserDefaults<\/code> ignores any attempt to set an object that is a dictionary or array bridged from Python.<\/p>","protected":false},"excerpt":{"rendered":"<p>David Smith (tweet): If you find yourself needing to do anything else to read a preference, you should take a step back and reconsider: caching values from NSUserDefaults is usually unnecessary, since it&rsquo;s extremely fast to read from. Calling -synchronize before reading a value is always unnecessary. Responding when the value changes is almost always [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2025-12-16T20:49:49Z","apple_news_api_id":"d879c354-729d-4390-ad95-b736a31e0773","apple_news_api_modified_at":"2025-12-16T20:49:49Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A2HnDVHKdQ5Ctlbc2ox4Hcw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,800,31,1137,275,30,1199,2868,54,138,71,53],"class_list":["post-13651","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-concurrency","tag-ios","tag-ios-9","tag-keyvalueobserving","tag-mac","tag-mac-os-x-10-11","tag-nsuserdefaults","tag-objective-c","tag-optimization","tag-programming","tag-sandboxing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=13651"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13651\/revisions"}],"predecessor-version":[{"id":13663,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13651\/revisions\/13663"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=13651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=13651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=13651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}