简体   繁体   中英

Issue with value conversion in iOS using NSUInteger and NSMutableDictionary

I have a NSMutableDictionary which holds several items in a key/value pairing. I have stored strings with a key and retrieved them without issue, however, when I store a value of type NSUInteger and try to fetch that back from the dictionary I end up with a really large value.

The first part of my if statement I check to see if the value I am looking for is not null or greater than zero. I am trying to basically hold a score or value in the dictionary that I can access so I can add to or take away from.

As you can see in the else statement I have two NSLog statements, this is where I see the difference in the value. The first NSLog statement displays a value of 100, as I would expect from clicking my 'Happy' button. However, once the value is retrieved back from the dictionary the currentTotal is 109709424. I am sure this has something to do with the format specifier I am using in the conversion of this integer value to a string. I tried storing this value, currentTotal, as an int or NSUInteger in my dictionary under the key "Total" but the conversion from a non objective-c type object to an 'id' is disallowed in ARC so I am stuck.

NSUInteger currentTotal = 0;
NSUInteger happinessValue = 100;
NSUInteger sadnessValue = 20; 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    if ([appDelegate.data valueForKey:@"Total"] != nil || [appDelegate.data valueForKey:@"Total"] > 0) {

        currentTotal = (int)[appDelegate.data valueForKey:@"Total"];

        if ([segue.identifier isEqualToString:@"Happy"]) {
            [segue.destinationViewController setHappiness:(int)happinessValue :segue.identifier];
            currentTotal = add(currentTotal, happinessValue);

            [appDelegate.data setObject:(NSString *)[NSString stringWithFormat: @"Value: %i", happinessValue] forKey:@"Value"];
        } else if ([segue.identifier isEqualToString:@"Sad"]) {
            [segue.destinationViewController setHappiness:(int)sadnessValue :segue.identifier];
            currentTotal -= [segue.destinationViewController setTotalValue:happinessValue];
            [appDelegate.data setObject:(NSString *)[NSString stringWithFormat: @"Value: %i", sadnessValue] forKey:@"Value"];
        }
    }
    else {

        if ([segue.identifier isEqualToString:@"Happy"]) {
            [segue.destinationViewController setHappiness:(int)happinessValue :segue.identifier];
            NSLog(@"Old value ELSE: %i", currentTotal);
            currentTotal = [segue.destinationViewController setTotalValue:happinessValue];
            NSLog(@"New value ELSE: %i", currentTotal);
            [appDelegate.data setObject:(NSString *)[NSString stringWithFormat: @"Value: %i", happinessValue] forKey:@"Value"];
        } else if ([segue.identifier isEqualToString:@"Sad"]) {
            [segue.destinationViewController setHappiness:(int)sadnessValue :segue.identifier];
            currentTotal = [segue.destinationViewController setTotalValue:happinessValue];
            [appDelegate.data setObject:(NSString *)[NSString stringWithFormat: @"Value: %i", sadnessValue] forKey:@"Value"];
        }

        NSLog(@"Current Total: %i", currentTotal);
        [appDelegate.data setObject:[NSString stringWithFormat:@"%d", currentTotal] forKey:@"Total"];
        NSLog(@"Total stored: %i", (int)[appDelegate.data valueForKey:@"Total"]);
    }

}

This looks like a problem:

currentTotal = (int)[appDelegate.data valueForKey:@"Total"];

-valueForKey: returns a pointer to an Objective-C object, not an int , so you cannot cast it to an int and expect anything useful to happen. If it is an NSNumber , then you need to do something like this:

currentTotal = [[appDelegate.data valueForKey:@"Total"] intValue];

See the docs on NSNumber for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM