Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-18-2010, 02:50 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default NSUserDefaults integerForKey

Hi Folks,

I am trying to get the Results from NSUserDefaults of the type "integer":

Code:
int found = [userSettings integerForKey:[NSString stringWithFormat:@"%@", writeCountry]];
As I have a switch like this:

Code:
switch (randomSummary) {
		
				NSLog(@"RandomSummary: %d", randomSummary);

			case 1:
				randomCountry = @"Achaia";
				writeCountry = @"achaia";
				[self attackSettings];
				break;
			case 2:
				randomCountry = @"Aegyptus";
				writeCountry = @"aegyptus";
				[self attackSettings];
				break;
			case 3:	
				randomCountry = @"Sardinia";
				writeCountry = @"sardinia";
				[self attackSettings];
				break;
default:
break;

}
The expected result is 0 or 1, but I get the result of (null) and thatīs why my app is crashing...

Code:
	if (found == 0) {
		
		rival = 1+ arc4random() % (60);
		
		ownStrenght = [userSettings floatForKey:@"MilitaryWert"];
		NSLog(@"ownStrenght: %.0f", ownStrenght);
		
}
Thanks for any help!

BR,

Stefan
StefanL is offline   Reply With Quote
Old 04-18-2010, 02:57 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by StefanL View Post
Hi Folks,

I am trying to get the Results from NSUserDefaults of the type "integer":

Code:
int found = [userSettings integerForKey:[NSString stringWithFormat:@"%@", writeCountry]];
As I have a switch like this:

Code:
switch (randomSummary) {
		
				NSLog(@"RandomSummary: %d", randomSummary);

			case 1:
				randomCountry = @"Achaia";
				writeCountry = @"achaia";
				[self attackSettings];
				break;
			case 2:
				randomCountry = @"Aegyptus";
				writeCountry = @"aegyptus";
				[self attackSettings];
				break;
			case 3:	
				randomCountry = @"Sardinia";
				writeCountry = @"sardinia";
				[self attackSettings];
				break;
default:
break;

}
The expected result is 0 or 1, but I get the result of (null) and thatīs why my app is crashing...

Code:
	if (found == 0) {
		
		rival = 1+ arc4random() % (60);
		
		ownStrenght = [userSettings floatForKey:@"MilitaryWert"];
		NSLog(@"ownStrenght: %.0f", ownStrenght);
		
}
Thanks for any help!

BR,

Stefan

Can you post the code that saves these values into user defaults? That's the side of the puzzle we are missing.

You have posted code that's trying to read both integer values and float values from user defaults. Are they both failing?


Regards,

Duncan C
WareTo
Duncan C is online now   Reply With Quote
Old 04-18-2010, 03:07 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default

Hi Duncan,

thanks for your reply! I am setting this variable in the beginning:

Code:
[userSettings setInteger:0 forKey:@"langobardi"];
This is actual the point where it is crashing...

The float variables and other integers from the NSUserDefaults are working fine. The error seems only to occurs with this key "langobardi"...

here I have the complete method:

Code:
-(void)attackSettings {
	
	NSUserDefaults *userSettings = [NSUserDefaults standardUserDefaults];
	
if (![userSettings integerForKey:@"Year"] == 0) {

	found = [userSettings integerForKey:[NSString stringWithFormat:@"%@", writeCountry]];
	
	NSLog(@"found = %@", found);
	
//IT IS CRASHING HERE, BECAUSE found is (null)...
	if (found == 0) {
		
		rival = 1+ arc4random() % (60);
		
		ownStrenght = [userSettings floatForKey:@"MilitaryWert"];
		NSLog(@"ownStrenght: %.0f", ownStrenght);
		
		if (rival > ownStrenght) {
			
			//You have lost
			
			[userSettings setInteger:0 forKey:[NSString stringWithFormat:@"%@", writeCountry]];
			
			
		} else if (rival < ownStrenght) {
			
			won = 1;
			
			//read Actual owned Countries:
			
			int ownCountries = [userSettings integerForKey:@"Countries"];
			ownCountries = ownCountries + 1;
			[userSettings setInteger:ownCountries forKey:@"Countries"];
			[userSettings setInteger:1 forKey:[NSString stringWithFormat:@"%@", writeCountry]];
		}
		
		if (won == 1) {
			
			residents = 3000+ arc4random() % (30000);
			int wonresidents = residents;
			readResidents = [userSettings integerForKey:@"Residents"];
			residents = residents + readResidents;
			NSLog(@"Residents: %d", residents);
			[userSettings setInteger:residents forKey:@"Residents"];
			
			attackText = [NSString stringWithFormat:@"You have been attacked by %@ but you won! You have conquered %@ and your population has been grown by %i! But Vengeance is near and you should take care! Donīt leave your army unattanded...\n\n", randomCountry, randomCountry, wonresidents];
			residentsLabel.text = [NSString stringWithFormat:@"%i", residents];
			[userSettings setInteger:1 forKey:[NSString stringWithFormat:@"%@", writeCountry]];
			NSLog(@"%@", attackText);
		} else {
			
			residents = 300+ arc4random() % (1000);
			int lostresidents = residents;
			readResidents = [userSettings integerForKey:@"Residents"];
			residents = readResidents - residents;
			[userSettings setInteger:residents forKey:@"Residents"];
			
			attackText=[NSString stringWithFormat:@"You have been attacked by %@ and you lost %0.i residents in this battle! You better prepare for battle and ride for %@ and war!\n\n", randomCountry, lostresidents, randomCountry];
			NSLog(@"%@", attackText);
			residentsLabel.text = [NSString stringWithFormat:@"%i", residents];
			[userSettings setInteger:0 forKey:[NSString stringWithFormat:@"%@", writeCountry]];
		}
		
	} else {
		
		rival = 1+ arc4random() % (60);
		
		ownStrenght = [userSettings floatForKey:@"MilitaryWert"];
		NSLog(@"ownStrenght: %.0f", ownStrenght);
		
		if (rival > ownStrenght) {
			
			[userSettings setInteger:0 forKey:[NSString stringWithFormat:@"%@", writeCountry]];
			
			residents = 100+ arc4random() % (1000);
			int lostresidents = residents;
			readResidents = [userSettings integerForKey:@"Residents"];
			residents = readResidents - residents;
			[userSettings setInteger:residents forKey:@"Residents"];
			
			attackText = [NSString stringWithFormat:@"You have been doomed! A rebellion in %@ and you failed! Your armies where not strong enough to smother the rebellion! You lost %i residents in the riot\n\n", randomCountry, lostresidents];
			NSLog(@"%@", attackText);
			
			[userSettings setInteger:0 forKey:[NSString stringWithFormat:@"%@", writeCountry]];
			residentsLabel.text = [NSString stringWithFormat:@"%i", residents];
			
			
		} else if (rival < ownStrenght) {
			
			residents = 100+ arc4random() % (1000);
			int lostresidents = residents;
			readResidents = [userSettings integerForKey:@"Residents"];
			residents = readResidents - residents;
			[userSettings setInteger:residents forKey:@"Residents"];
			
			attackText = [NSString stringWithFormat:@"A rebellion in %@ but you smother it! You have conquered %@ but you have lost %i residents in the battle!\n\n", randomCountry, randomCountry, lostresidents];
			
			
			NSLog(@"%@", attackText);
			residentsLabel.text = [NSString stringWithFormat:@"%i", residents];
			[userSettings setInteger:1 forKey:[NSString stringWithFormat:@"%@", writeCountry]];
			
		}
	

		}
	}
}
Thanks for your assistence!

BR,
Stefan
StefanL is offline   Reply With Quote
Old 04-18-2010, 04:01 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by StefanL View Post
Hi Duncan,

thanks for your reply! I am setting this variable in the beginning:

Code:
[userSettings setInteger:0 forKey:@"langobardi"];
This is actual the point where it is crashing...

The float variables and other integers from the NSUserDefaults are working fine. The error seems only to occurs with this key "langobardi"...


--snip--

Stefan
Your description is kind of scattered, so I don't really understand what code is being run, and what's failing.

Here's what I would suggest you do:

Write code in appDidFinishLaunching that sets all your userdefaults values at startup, then calls synchronize, and quits. That way all your user defaults will get saved.

Run your app in debug on the simulator.

Look in ~/Library/Application Support/iPhone Simulator/User/Applications/
for the most recently changed directory. That should be the directory for your app. Inside that directory, you should see a subdirectory /Library, and inside that, you should see a subdirectory /Preferences. In that subdirectory will be a plist file. Double-click the plist file to open it in the property list editor. That will let you see the keys in your preferences file. My guess is that you are trying to read one of your user defaults settings without saving it first.


Regards,

Duncan C
WareTo
Duncan C is online now   Reply With Quote
Old 04-18-2010, 05:05 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: Wien/Austria
Posts: 242
StefanL is on a distinguished road
Default

Hi Duncan,

thanks a lot for your support! I am really sorry, that I have "stolen your time" with this stupid thread (I really do not mean that sarcastic)!

The solution was quite simple and occours because of this NSLog:

the orignial was:

NSLog(@"found %@", found) - but found is an integer - therefore it should be:

NSLog(@"found Value of %@ = %d", writeCountry, found);

thanks again for your support and your help!

BR,

Stefan
StefanL is offline   Reply With Quote
Reply

Bookmarks

Tags
int, nsuserdefaults

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 323
21 members and 302 guests
baja_yu, cgokey, chemistry, Domele, Duncan C, Fstuff, gbenna, givensur, heshiming, HowEver, iAppDeveloper, iphonedevshani, jbro, JoeRCruso, mdpauley, n00b, newDev, seokwon lee, SLIC, stanny, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,874
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:31 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0