0

I've to run this method on background or in new thread. How to pass two arguments in performSelector?

[self addMessageFromRemoteNotification:userInfo updateUI:NO];

-(void)addMessageFromRemoteNotification:(NSDictionary *)userInfo updateUI:(BOOL)updateUI
{
}
dineshsurya
  • 215
  • 1
  • 10
  • This will help you. http://stackoverflow.com/questions/2716143/sel-performselector-and-arguments/9108934#9108934 – yunas May 28 '13 at 14:07
  • see my answer to this question: http://stackoverflow.com/questions/16731105/how-can-i-pass-multiple-objects-to-a-delayed-method/16731378#16731378 – pmk May 28 '13 at 14:10

3 Answers3

1

If you must use performSelector and you want N parameters, simply wrap them in an array or dictionary and make the signature of the called method have a single parameter of NSArray or NSDictionary. Any primitive types (like int, float, etc.) will need to be wrapped in NSNumber.

Example With NSArray:

- (void) addMessageFromRemoteNotification:(NSArray*)parameters
{
}

...

[self performSelector:@selector(addMessageFromRemoteNotification:)
withObject:@[ obj1, obj2, obj3, @(4.0f)]];

Example With NSDictionary:

- (void) addMessageFromRemoteNotification:(NSDictionary*)parameters
{
}

...

[self performSelector:@selector(addMessageFromRemoteNotification:)
withObject:@{ @"prop1": @"prop1value", @"prop2": @(4.0f) }];

Good luck!

jjxtra
  • 20,415
  • 16
  • 100
  • 140
0

there is method

[self performSelector:@selector(addMessageFromRemoteNotification:) withObject:param1 withObject:param2];

you can use this.

0

Just create a model class for the data you want to send, eg -

@interface Car : NSObject

@property (nonatomic, strong) NSString *manufacturer;
@property (nonatomic, strong) NSString *colour;
@property (nonatomic, strong) NSDate *year;

@end

Then pass an instance of it in your performSelector: method.

It's often better than passing multiple parameters anyway, in case you decide to change the data model (eg, add more properties) - though it will depend on the circumstances of course.

SomaMan
  • 4,127
  • 1
  • 34
  • 45
  • This isn't wrong, but it is a little heavy-handed; just use a dictionary... that is what the framework does everywhere: `NSDictionary * userInfo` – Grady Player May 28 '13 at 14:59
  • Also probably a matter of programming style, or if the parameters suit it (eg, if you're often using multiple parameters in your code, would a dedicated model make things clearer) – SomaMan May 29 '13 at 02:19