Bizarre Settings.bundle/UserDefaults behavior

Discussion in 'iOS Programming' started by RossOliver, Aug 3, 2008.

  1. macrumors regular

    Joined:
    Nov 6, 2006
    #1
    Hey,

    I recently discovered that my program was crashing due to something in the settings setup. If you compile and run my application it will crash, but if you compile it, then go into settings->my prog and then back to the home screen and run it, it behaves correctly (until you reset the simulator settings). This would indicate there is a problem with the DefaultValue of one of my settings - but I can't see how...

    The error message it's throwing at me doesn't really give me any clues:

    Code:
    [Session started at 2008-08-03 22:12:33 +0100.]
    2008-08-03 22:12:35.650 MyProg[65241:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
    2008-08-03 22:12:35.652 MyProg[65241:20b] Stack: (
        2417258827,
        2455802107,
        2417258283,
        2417258346,
        2529103647,
        2528569528,
        18634,
        21758,
        12663,
        15924,
        816648952,
        816654649,
        11232,
        816177758,
        816558114,
        816559338,
        816556080,
        816274443,
        816209415,
        816206378,
        587042,
        596108,
        2416760341,
        2416762104,
        589112,
        589309,
        816175835,
        816221412,
        9880,
        9734
    )
    
    [Session started at 2008-08-03 22:12:35 +0100.]
    Loading program into debugger…
    GNU gdb 6.3.50-20050815 (Apple version gdb-960) (Sun May 18 18:38:33 UTC 2008)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-apple-darwin".warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found).
    warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
    warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found).
    warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory).
    Program loaded.
    sharedlibrary apply-load-rules all
    Attaching to program: `/Users/Ross/Library/Application Support/iPhone Simulator/User/Applications/40B1AFCF-FEB1-4F1B-B3B1-413A18ADA18A/MyProg.app/MyProg', process 65241.
    kill
    error while killing target (killing anyway): warning: error on line 1980 of "/SourceCache/gdb/gdb-960/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)
    quit
    
    The Debugger has exited with status 0.(gdb) 
    
    The only thing I have changed in my Settings.bundle is the Root.plist:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    	<key>Title</key>
    	<string>MyProg</string>
    	<key>StringsTable</key>
    	<string>Root</string>
    	<key>PreferenceSpecifiers</key>
    	<array>
    		<dict>
    			<key>Type</key>
    			<string>PSGroupSpecifier</string>
    			<key>Title</key>
    			<string>Section One</string>
    		</dict>
    		<dict>
    			<key>Type</key>
    			<string>PSSliderSpecifier</string>
    			<key>Key</key>
    			<string>preference_one</string>
    			<key>DefaultValue</key>
    			<integer>5</integer>
    			<key>MinimumValue</key>
    			<integer>1</integer>
    			<key>MaximumValue</key>
    			<integer>5</integer>
    			<key>MinimumValueImage</key>
    			<string></string>
    			<key>MaximumValueImage</key>
    			<string></string>
    		</dict>
    		<dict>
    			<key>Type</key>
    			<string>PSSliderSpecifier</string>
    			<key>Key</key>
    			<string>preference_two</string>
    			<key>DefaultValue</key>
    			<integer>15</integer>
    			<key>MinimumValue</key>
    			<integer>1</integer>
    			<key>MaximumValue</key>
    			<integer>60</integer>
    			<key>MinimumValueImage</key>
    			<string></string>
    			<key>MaximumValueImage</key>
    			<string></string>
    		</dict>
    		<dict>
    			<key>Type</key>
    			<string>PSMultiValueSpecifier</string>
    			<key>Title</key>
    			<string>Preference Three</string>
    			<key>Key</key>
    			<string>preference_three</string>
    			<key>DefaultValue</key>
    			<integer>5</integer>
    			<key>Values</key>
    			<array>
    				<integer>1</integer>
    				<integer>2</integer>
    				<integer>3</integer>
    				<integer>4</integer>
    				<integer>5</integer>
    				<integer>6</integer>
    				<integer>7</integer>
    				<integer>8</integer>
    			</array>
    			<key>Titles</key>
    			<array>
    				<string>One</string>
    				<string>Two</string>
    				<string>Three</string>
    				<string>Four</string>
    				<string>Five</string>
    				<string>Six</string>
    				<string>Seven</string>
    				<string>Eight</string>
    			</array>
    		</dict>
    		<dict>
    			<key>Type</key>
    			<string>PSMultiValueSpecifier</string>
    			<key>Title</key>
    			<string>Preference Four</string>
    			<key>Key</key>
    			<string>preference_four</string>
    			<key>DefaultValue</key>
    			<integer>5</integer>
    			<key>Values</key>
    			<array>
    				<integer>1</integer>
    				<integer>2</integer>
    				<integer>3</integer>
    				<integer>4</integer>
    				<integer>5</integer>
    				<integer>6</integer>
    				<integer>7</integer>
    				<integer>8</integer>
    			</array>
    			<key>Titles</key>
    			<array>
    				<string>One</string>
    				<string>Two</string>
    				<string>Three</string>
    				<string>Four</string>
    				<string>Five</string>
    				<string>Six</string>
    				<string>Seven</string>
    				<string>Eight</string>
    			</array>
    		</dict>
    		<dict>
    			<key>Type</key>
    			<string>PSGroupSpecifier</string>
    			<key>Title</key>
    			<string>Section Two</string>
    		</dict>
    		<dict>
    			<key>Type</key>
    			<string>PSToggleSwitchSpecifier</string>
    			<key>Title</key>
    			<string>Preference Five</string>
    			<key>Key</key>
    			<string>preference_five</string>
    			<key>DefaultValue</key>
    			<true/>
    			<key>TrueValue</key>
    			<true/>
    			<key>FalseValue</key>
    			<false/>
    		</dict>
    		<dict>
    			<key>Type</key>
    			<string>PSToggleSwitchSpecifier</string>
    			<key>Title</key>
    			<string>Preference Six</string>
    			<key>Key</key>
    			<string>preference_six</string>
    			<key>DefaultValue</key>
    			<true/>
    			<key>TrueValue</key>
    			<true/>
    			<key>FalseValue</key>
    			<false/>
    		</dict>
    	</array>
    </dict>
    </plist>
    
    I access the preferences using:

    Code:
    MyProgAppDelegate *myProgAppDelegate = ( MyProgAppDelegate * )[[UIApplication sharedApplication] delegate];
    	
    NSUserDefaults *userDefaults = [myProgAppDelegate preferences];
    
    printf( "%i", [userDefaults integerForKey:@"preference_whatever"] );
    
    If anyone can spot where I have gone wrong I would be very greatful...

    Thanks for your time,

    -Ross
     
  2. thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #2
    Having looked at the AppPrefs example again, it would appear I am missing a step where I actually need to register the default Settings.bundle values with the UserDefaults - I was under the impression specifying a DefaultValue attribute would provide this behaviour :rolleyes:

    -Ross
     
  3. macrumors member

    Joined:
    Jun 11, 2008
    #3
    Hi one solution can be as follows which i hve done and it works fine fr me
    write following statements
    NSString *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:preference_one];
    if (testValue == nil)
    {
    NSLog(@"Setting application not yet chaged for this app");
    initialize all ur default value variables without using
    NSUserDefaults
    }
    else
    {
    // we're ready to do, so lastly set the key preference values
    self.variablename = [[NSUserDefaults standardUserDefaults] stringForKey:preference_one];
    }
     

Share This Page