Ser Pounce
Ser Pounce

Reputation: 14553

Syntax for Extracting CGFloat out of NSDictionary

Just wondering what the syntax would be to extract a CGFloat out of an NSDictionary like follows:

slider.minimumValue = [filterAttributes valueForKey:kCIAttributeSliderMin];

Upvotes: 2

Views: 3812

Answers (2)

Dave
Dave

Reputation: 7717

You can only put OBJECTS into an NSDictionary (or NSARRAY). CGFloat is a literal (just maps to a float), so you can't put it into or retrieve it from the dictionary.

Instead, wrap it as an NSNumber (when you add it to the dictionary), which is an object.

NSNumber *sliderMin = [NSNumber numberWithFloat:kCIAttributeSliderMin]

Or using the new syntax, you can just say @kCIAttributeSliderMin or @(kCIAttributeSliderMin) to autobox as an NSNumber.

To get the value back out, you'll retrieve the object as an NSNumber then say, [myNumber floatVal] to get the NSFloat.

Finally, you probably want to say "objectForKey" not "valueForKey".

update - sorry, in your example you're treating kCIAttributeSliderMin as a key, and I'm using it as the 'value'; but I think you get the point. Store an NSNumber object; retrieve an NSNumber object. Sorry for any confusion swapping that may have caused.

Upvotes: 7

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

An NSDictionary only holds objects. What kind of object would wrap a primitive like CGFloat? NSNumber would make sense. Now, since CGFloat is either a float or a double, you'll probably want to get the double value to preserve precision/range.

Therefore:

slider.minimumValue = [[filterAttributes valueForKey:kCIAttributeSliderMin] doubleValue];

Upvotes: 7

Related Questions