Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with value conversion in iOS using NSUInteger and NSMutableDictionary
    primarykey
    data
    text
    <p>I have a <code>NSMutableDictionary</code> 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 <code>NSUInteger</code> and try to fetch that back from the dictionary I end up with a really large value.</p> <p>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.</p> <p>As you can see in the else statement I have two <code>NSLog</code> statements, this is where I see the difference in the value. The first <code>NSLog</code> 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.</p> <pre><code>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"] &gt; 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"]); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload