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 Tying it All Together Cumulative Review

Sam Adams
Sam Adams
1,213 Points

Struggling to convert the NSNumbers from an NSArray into floats that can then be used with other floats.

I may have completely missed the idea here, but I really wanted to give it a go before asking for assistance. Thanks in advance!

variable_assignment.mm
float average = 0;
float runningTotal = 0;

NSArray *temps = @[@75.5, @83.3, @96, @99.7];

for (NSNumber *values in temps){
  runningTotal = runningTotal + (values @[floatValue]);  
}

average = runningTotal / temps.count;

1 Answer

floatValue is an instance property of NSNumber, so if you have an NSNumber instance, say, number, you may access the raw float value of that number with either

  1. Property dot-notation: number.floatValue, or
  2. The older-style Objective-C bracket method notation [number floatValue]

Looks like you are a little bit confused about the @[ ... ] notation as well. When you wrap a list of comma-separated objects within @[ ... ], you are creating an NSArray containing those objects.