performSelector may cause a leak because its selector is unknown

后端 未结 19 2211
小蘑菇
小蘑菇 2020-11-22 01:54

I\'m getting the following warning by the ARC compiler:

\"performSelector may cause a leak because its selector is unknown\".

Here\'s what

19条回答
  •  梦如初夏
    2020-11-22 02:43

    Matt Galloway's answer on this thread explains the why:

    Consider the following:

    id anotherObject1 = [someObject performSelector:@selector(copy)];
    id anotherObject2 = [someObject performSelector:@selector(giveMeAnotherNonRetainedObject)];
    

    Now, how can ARC know that the first returns an object with a retain count of 1 but the second returns an object which is autoreleased?

    It seems that it is generally safe to suppress the warning if you are ignoring the return value. I'm not sure what the best practice is if you really need to get a retained object from performSelector -- other than "don't do that".

提交回复
热议问题