Skip to main content

Posts

Showing posts with the label nsuserdefaults

Can"t do mathematical operations with int from NSUserDefaults

i have integer data, stored in NSUserDefaults, there is my code: - (IBAction)addButton:(id)sender { NSInteger oldValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"myValue"]; NSString *string1=[addTextField text]; int add = [string1 floatValue]; int new = globalCalories; int old=oldValue; if(recomended.text==[NSString stringWithFormat:@"%i", oldValue]){ **self.day = (int)roundf(old-add); dayLeft.text=[NSString stringWithFormat:@"%d", oldValue-add]; }** else { self.day=(int)roundf(new-add); dayLeft.text=[NSString stringWithFormat:@"%d", day]; } } I copied all of button action code, just in case, but i did mark with bold strings of code, that appear to not work. So, it suppose to do mathematical operations with stored data (oldValue), but when i launch programs, it dosnt, in fact, it does, but instead of valid value program "think" that oldValue is

how to pass NSUserDefaults integer to a UInt32 object

I am wondering if it is possible to pass an integer value I am creating in my NSUserDefaults into a UInt32 object? So far I am creating the NSUserDefaults as below. NSString * yourKey = @"RequestNumber"; NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; [defaults setInteger:[defaults integerForKey:yourKey] + 1 forKey:yourKey]; and then I am trying to pass the user defaults value into my object as below.. However I am fairly positive I am completely wrong... which is why im here :) any help would be appreciated. UInt32 *requestNumber = [[NSUserDefaults standardUserDefaults] integerForKey:yourKey];