Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

You should call

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

instead (note the colon behind the message name).

Did you mean

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

Assuming myMethod: is a method that takes an object?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top