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

前端 未结 4 503
后悔当初
后悔当初 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:27

    As described above you get that warning because the compiler does not know where (or if) to put the retain/release of the performSelector: return value.

    But note that if you use [someObject performSelector:@selector(selectorName)] it will not generate warnings (at least in Xcode 4.5 with llvm 4.1) because the exact selector is easy to be determined (you set it explicitly) and that's why compiler is able to put the retain/releases in the correct place.

    That's why you will get warning only if you pass the selector using SEL pointer because in that case the compiler is unable to determine in all case what to do. So using the following

    SEL s = nil;
    if(condition1) SEL = @selector(sel1)
    else SEL = @selector(sel2)
    
    [self performSelector:s];
    

    will generate warning. But refactoring it to be:

    if(condition1) [self performSelector:@selector(sel1)]
    else [self performSelector:@selector(sel2)]
    

    will not generate any warnings

提交回复
热议问题