Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Stephen Hancocks
9,271 PointsCompare a property of an object in an array with a static number
I am really struggling to perform an 'if' statement on an array of data that I have parsed from HTML. I know the data is correct and the NSMutableArray contains 20 or so objects with properties "month" and "credits".
I want to check whether the credits are within certain ranges. I'm getting all sorts of errors
if([[_Credits objectAtIndex:18] valueForKey:@"credits"] > 10)
gives the errors "Ordered comparison between pointer and integer ('id' and 'int')" "Implicit conversion of 'int' to 'id' is disallowed with ARC
As the properties of my array objects are strings I thought it would help if I converted them to NSNumber. The conversion works fine [although it seems very long winded - a lot of the code I find on forums is for older iOS's]
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:@"###0.###"];
NSNumber *formattedStringNumber = [numberFormatter numberFromString:[[_credits objectAtIndex:18] valueForKey:@"credits"]];
NSLog(@"formattedNumberString: %@", formattedStringNumber);
Trying to compare formattedStringNumber throws up these issues
if(formattedStringNumber > 10)
Implicit conversion of 'int' to NSNumber * is disallowed with ARC and Order comparison between pointer and integer ('NSNumber *' and 'int')
I really thought [and still hope] that there is an easily solution in my issue as I want to compare a lot of data against set targets.
Many thanks
Steve
5 Answers
eirikvaa
18,015 PointsCheck out this method from the NSString class:
- (int)intValue
It returns the integer value of the receiver's text.
You could use this method to convert the string object from your array to an integer value and then compare it. Hopefully no need for NSNumber objects.
Example:
NSString *string = @"5";
int value = [string intValue];
Stone Preston
42,016 Pointsif(formattedStringNumber > 10)
try using this
if(formattedStringNumber > @10)
which converts it to an NSNUmber literal.
Stephen Hancocks
9,271 PointsJust tried that. It gave a warning
'Direct comparison of a numeric literal has undefined behaviour'
and when I ran my code incorrectly as it returned that formattedStringNumber [2.5] was greater than 10.
Stone Preston
42,016 Pointsok try using
if([formattedStringNumber intValue] > 10)
that will convert your nsnumber to an int, so you can use the comparison operator on it.
Stone Preston
42,016 Pointsok try using
if([formattedStringNumber intValue] > 10)
that will convert your nsnumber to an int, so you can use the comparison operator on it.
Stephen Hancocks
9,271 PointsI've removed the error by replacing the static number with a NSNumber variable that I declared.
NSNumber *monthOne = @50;
if(formattedStringNumber > monthOne)
I would still like a way to convert my array object property to an NSNumber in less than 4 lines of code. Can this be shortened at all?
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:@"###0.###"];
NSNumber *formattedStringNumber = [numberFormatter numberFromString:[[_personalCaseCredits objectAtIndex:18] valueForKey:@"caseCredits"]];
NSLog(@"formattedNumberString: %@", formattedStringNumber);
Stephen Hancocks
9,271 PointsStephen Hancocks
9,271 PointsThanks. Just tried that [although with floatValue as I need decimal places] and it's work a treat. Three lines of code down to one is just what I wanted.
Thanks
eirikvaa
18,015 Pointseirikvaa
18,015 PointsNo problem. The feeling of finding the exact method you need is always good!