Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 525521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:40:50+00:00 2026-05-13T08:40:50+00:00

I’ve just added a settings bundle to my app and am having trouble reading

  • 0

I’ve just added a settings bundle to my app and am having trouble reading the bool settings. I know that upon launch of the app, the settings are not read unless the user actually enters them – and that’s what I am trying to capture.

However, my code is simply capturing if the answer is NO OR they havent been set. I need to find out if they’ve been set, THEN set answers!

setting code:

BOOL playSound;
BOOL playVibrate;


//test for some defaults
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs boolForKey:@"pref_sound"]) {
    playSound = YES;
    playVibrate = YES; 
} else {
    playSound = [prefs boolForKey:@"pref_sound"];
    playVibrate = [prefs boolForKey:@"pref_vibrate"]; 
}


if (playSound) {
//do stuff
}

the problem is, if the user sets the settings to “NO”, the code then changes both vibrate AND sound to yes – which is meant to be the capture for NOT setting….

any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:40:50+00:00Added an answer on May 13, 2026 at 8:40 am

    First of all you have a bug in your if conditional. You’re checking the boolean value itself which, according to the boolForKey: documentation, will return a NO if it’s not set yet. So boolForKey: is not the right way to do that.

    Here’s two other ideas.

    Consider using another setting with another key to specify whether your settings have been initialized. Check it when you launch your app, or when you first read a setting. Initialize if needed. For instance:

    - (void) initializeUserDefaults {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if (nil == [defaults objectForKey:@"initialized_defaults"]) {
            [defaults setBool:YES forKey:@"pref_sound"];
            [defaults setBool:YES forKey:@"pref_vibrate"];
            [defaults setObject:@"dummy_value" forKey:@"initialized_defaults"];
        }
    }
    

    A simpler solution (but I’m not sure if this would work) would be to change your conditional to read:

    if (![prefs objectForKey:@"pref_sound"]) {
    

    Again I don’t know if this will do what I imagine it will, but I imagine that objectForKey: will return the underlying boxed boolean object, if it’s there, or nil.

    If you add a new setting in a new version of your app, you don’t want to leave your new settings uninitialized and you don’t want to stomp your users’ existing settings. This second method makes that effortless, and in the first method it’s easier to screw up. But the first method also gathers all your settings in one place so you can see how it’s supposed to work, which I like.

    I am not sure what ADC docs would consider a best practice though. To find that out, I’d suggest you look at any code samples referenced from the NSUserDefaults class reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.