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 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.