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

iOS Object-Oriented Objective-C Memory, Arrays and Loops, Oh My! NSArrays and NSMutableArrays in Practice

Why don't we use the floatValue method to unpack the float from NSNumber *quizScore?

As per above

2 Answers

To answer your question the reason he used int was because none of the values contained decimals all of them were int values (90, 80, etc instead of 90.3, 80.4) then he stored them into a float variable because he was taking the average which would be a decimal. Otherwise he would of used floatValue or doubleValue instead of intValue.

Hi Martin,

I understand using an Int because you don't need the precision at that point. But doesn't doing it that way essentially cause the value to be cast twice?

First we make the Object an Int. Then it's (promoted?) to Float to compute the value of ScoreTotal?

It would seem making it a Float in the first place would bypass the additional conversion.

  for ( NSNumber *grade in quizArray){
           scoreTotal += [grade floatValue];    
    }

Or am I over-thinking this?

Thanks Martin, but the quizScore variable is a float so the rest of our code should be consistent with this.