2

I want to do performSelector:withObject: but where the object is a CGFloat. So it's not actually an object. How can I do this?

the object I am performing the selector on is not mine, I can't modify it.

eg

[xyz performSelector:@selector(setOffset:) withObject:2];

(after posting I changed what I need to slightly to this:

[xyz performSelector:@selector(setOffset:) withObject:CGSizeMake(2,0)];
Jonathan.
  • 53,997
  • 54
  • 186
  • 290

5 Answers5

3

If you're trying to invoke an arbitrary selector against an object you don't have control over, you could use an NSInvocation to set up the selector, target, and arguments, and obtain the return value after the method has been executed.

Generally, though, there are simpler solutions.

Justin Spahr-Summers
  • 16,893
  • 2
  • 61
  • 79
  • 2
    Look at the code in this question: http://stackoverflow.com/questions/2716143/sel-performselector-and-arguments to see how to set up and use NSInvocation with a non-object type. – Kendall Helmstetter Gelner Dec 05 '10 at 21:26
1

try use IMP (A pointer to the start of a method implementation.)

SEL setOffsetSEL = @selector(setOffset:);
IMP setOffsetIMP = [XYZClass instanceMethodForSelector:setOffsetSEL];
setOffsetIMP(xyz, setOffsetSEL, 2);
Joe Yang
  • 1,615
  • 14
  • 10
0

You need an object to message. When I've needed to do something like this, I'll create a simple container class, shove the data in an instance and then perform a selector (often @selector(doIt:)) when needed.

If you can target 4.x, you can use blocks for this, too, typically.

(Without knowing more about what exactly you are trying to do, hard to get any more specific.)

bbum
  • 162,346
  • 23
  • 271
  • 359
  • 1
    I think you have misunderstood. It sounds like he has an object to message, but the argument to the object's method (which you'd normally pass as the "with object" argument) is a float. – Chuck Dec 05 '10 at 21:02
0

object passed is a CGFloat. So it's not actually an object.

As you wrote immediately after, if you're passed an object, it can't be a CGFloat, as CGFloat is a typedef'ed primitive (float or double).

If you've been passed a number value as an object, likely you were passed an NSNumber somehow.

With zero context to your question, there's no way to be sure.

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • Sorry I think the word "passed" made it confusing. I want to do `[thing performSelector:@selector(someThing:) withObject:1.0f]` – Jonathan. Dec 05 '10 at 21:05
-3

You can use:

[NSNumber numberWithFloat:(float)value]
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Luca Bernardi
  • 4,191
  • 2
  • 31
  • 39
  • 1
    This will spectacularly not work. A function expecting a float cannot detect that you have passed an NSNumber and unwrap it for you. So you'll get a garbage float with the same bit pattern as the pointer's value. – Chuck Dec 05 '10 at 21:00
  • 2
    I assumed that the selector was created by the user and the only problem was the performSelector:withObject: call – Luca Bernardi Dec 05 '10 at 21:04
  • If you edit your answer right now I will undown vote it, just add a full stop or something. – Jonathan. Dec 05 '10 at 21:11
  • @Jonathan: I edited the answer, so you should be able to undo your vote. – Bill the Lizard Dec 05 '10 at 22:50