user306766
user306766

Reputation:

performSelector won't pass object to the selector

I have an NSSet-object that I want to pass in a performSelector-call:

NSSet *myNSSetObject = [self getSet];

if (myNSSetObject.count != 1)
    return;

[self performSelector:@selector(myMethod:) withObject:myNSSetObject afterDelay:0.5];

Problem is that the Object arrives empty in myMethod. As performSelector retains the passed Object I don't understand what's going wrong.

My Method is defined as follows

- (void)myMethod:(NSSet *)myNSSetParam {
   NSLog(@"Set count: %d", myNSSetParam.count);
   [do sth];
}

The NSLog shows: Set count: 0


Looking deeper into the matter and already suggested by the above count, the NSSet object is actually passed, but looses it's record(s).

Is it not that an NSSet retains its objects?

Upvotes: 0

Views: 2117

Answers (5)

dreamlax
dreamlax

Reputation: 95335

Are you sure that getSet returns an actual NSSet and not a mutable subclass hiding under an NSSet “base pointer”? If it is mutable, it is possible for the set to be modified within the 0.5 second delay. Even if performSelector:withObject:afterDelay: retains the set, it does not prevent it from being modified.

Upvotes: 0

marcus
marcus

Reputation: 2531

After returning from the first message (or in part below the posted code snip), do you by accident alter the NSSet delivered by [self getSet]? If you remove objects from the set afterwards,myMethod: will not be able to access it anymore.

Upvotes: 1

Shaggy Frog
Shaggy Frog

Reputation: 27601

Did you mean

[self performSelector:@selector(myMethod:) withObject:myNSSetObject afterDelay:0.5]

Assuming myMethod: is a method that takes an object?

Upvotes: 0

Eiko
Eiko

Reputation: 25632

You should call

[self performSelector:@selector(myMethod:) withObject:myNSSetObject afterDelay:0.5];

instead (note the colon behind the message name).

Upvotes: 0

Justin Spahr-Summers
Justin Spahr-Summers

Reputation: 16973

Does myMethod actually take an argument? It's important to note that method names and selectors in Objective-C actually include the colons that are present in the declaration; therefore, a method like this:

 - (void)myMethod:(NSSet *)set;

must be referenced with @selector(myMethod:), including the trailing colon.

Upvotes: 1

Related Questions