SimonRH
SimonRH

Reputation: 1459

Accessing data from an NSDictionary

I'm using the following method to access data about an object. The first NSLog shows all of the data. The second NSLog shows the 'frame' data which comes out as: NSRect: {{168, 102}, {5, 5}}

How can I access the first set of coordinates from the NSRect and then the abscissa from the first pair?

-(void) moveTheShape:(NSTimer*)timer
{
    NSDictionary *userInfo = [timer userInfo];
    NSLog(@"Info:  %@",userInfo);
    //Info:  <Shape: 0x68b6c30; frame = (151 352; 5 5); layer = <CALayer: 0x68b6c00>>
    NSDictionary *frame = [userInfo  valueForKey:@"frame"];
    NSLog(@"frame: %@", frame);
    //NSRect: {{168, 102}, {5, 5}}
}

CORRECT SOLUTION:

-(void) moveTheShape:(NSTimer*)timer
{
Shape *userInfo = [timer userInfo];
NSLog(@"Info:  %@",userInfo);
CGPoint origin = userInfo.frame.origin;
NSLog(@"result: %f", origin.x);
}

Upvotes: 0

Views: 747

Answers (4)

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

The CGRect is stored as NSValue You will need to get the CGRectValue from the NSValue

Use The following

NSValue *value = [userInfo valueForKey:@"frame"];
CGRect rect = [value CGRectValue];

Upvotes: 2

Wevah
Wevah

Reputation: 28242

If my hunch about the type of userInfo is correct:

-(void) moveTheShape:(NSTimer*)timer
{
    Shape *userInfo = [timer userInfo];
    CGPoint origin = userInfo.frame.origin;
    // Do stuff with origin
}

Upvotes: 1

CodaFi
CodaFi

Reputation: 43330

Ha, abscissa, my new word for the day!

Anyhow, do you mean CGRect, as an iPhone uses CoreGraphics, not FoundationKit for all UIKit rectangles.

As for the accessing the struct's internal vars (CGPoint and CGSize to be exact), just use regular old dot notation after storing the frame into a CGRect var like so:

NSValue *value = [userInfo objectForKey:@"frame"];
CGRect frameVar = [value CGRectValue];
frameVar.origin = CGPointMake(someCoord, someOtherCoord);

Upvotes: 0

rdelmar
rdelmar

Reputation: 104092

int abscissa = frame.origin.x; will get you the x value. You use frame.size.width and frame.size.height to get the sizes.

Upvotes: 0

Related Questions