Did the Target-Action design pattern became bad practice under ARC?

前端 未结 4 507
后悔当初
后悔当初 2021-01-30 11:17

For years I\'ve been following a great pattern called Target-Action which goes like this:

An object calls a specified selector on a specified target object when the time

4条回答
  •  既然无缘
    2021-01-30 11:24

    ARC is throwing the warning because it can't guarantee that the selector isn't creating an object it doesn't know about. You could theoretically receive something from that method that ARC can't handle:

    id objectA = [someObject performSelector:@selector(createObjectA)];
    

    Maybe someday it can, but right now it can't. (Note if it does know the object (it's not an id) it doesn't throw this warning).

    If you're trying to simply execute a method without receiving an object back from it, I recommend using objc_msgSend. But you've gotta include in your class:

    #include 
    objc_msgSend(someObject, action);
    

提交回复
热议问题