Pass struct to performSelector: withObject: afterDelay:

I have a structure position, for example:

typedef struct Position { int x, y; } Position;

How can I pass it to NSObject performSelector:withObject:afterDelay:? Like this:

Position pos;
pos.x = pos.y = 1;
[self performSelector:@selector(foo:)
           withObject:pos               // ERROR
           afterDelay:5.0f];

EDIT: modified typo correction code

+5
source share
5 answers

Uhm .. use CGPoint and something like

[self performSelector:@selector(foo:) 
           withObject:[NSValue valueWithCGPoint:CGPointMake(pos.x, pos.y)] 
           afterDelay:5.0f];

And read it again as

NSValue v;
CGPoint point = [v CGPointValue];

or completely leave the Position class, CGPoint does the same

+9
source

You can wrap your own type using a class NSValue. The error is that you did not provide an object reference for the method.

Try using the class method NSValue +(NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;. Alternatively, you can get the value with -(void)getValue:(void *)buffer;.

+3
source

preformSelector:withObject: , foo: . :

  • NSValue .
0
source

Full answer based on user756245 (which doesn't tell you how to use it, not much help). In addition, Apple suggests using a slightly different method these days, IIRC:

typedef myStruct { int a; } myStruct;

myStruct aLocalPointer = ... assign to it

[self performSelector:@selector(myOtherMethod:) withObject:[NSValue value:&aLocalPointer withObjCType:@encode(myStruct)] afterDelay:0.25];
0
source

This most likely requires trouble, but you can convey CGPointhow idby connecting it this way:

withObject:(__bridge id)((void *)(&point))

This will crash if it pointgoes out of scope and your selector tries to read it.

0
source

All Articles