扫描 Objective-C 中块中的"self"调用实例?



是否有任何脚本(甚至是正则脚本)可以检查整个项目中的" self"或" self"或下划线参数的使用?

在移动代码时可能会偶然浏览这些,我想知道是否有人有更防弹的方法来避免它们。


编辑:例如,函数a应该抛出标志,而不是函数b ...仅在^{}中,在这种情况下它至关重要。

- (void)a
{
    ^
    {
        NSLog(@"This should throw a flag: %@ ... %@ ... %@", [self someProperty], _someProperty, self.someProperty);
        ^(NSInteger number)
        {
            NSLog(@"This should also throw a flag: %@ ... %@ ... %@", [self someProperty], _someProperty, self.someProperty);
        }(0);
    }
}
- (void)b
{
    NSLog(@"This is OK: %@ ... %@ ... %@", [self someProperty], _someProperty, self.someProperty);
    __weak NSObject *weakSelf=self;
    ^
    {
        NSLog(@"This is also OK.", [weakSelf someProperty], weakSelf, weakSelf.someProperty);
        ^(NSInteger number)
        {
            NSLog(@"This, too, is OK.", [weakSelf someProperty], weakSelf, weakSelf.someProperty);
        }(0);
    }
}

我不确定这是您的意思,但是您可以让编译器通过将其放入'构建中,以对'self'的每一次使用'self'的每种用法提出警告三角形阶段|运行目标的脚本:

TAGS="self"
find "${SRCROOT}/" -type f ( -name "*.h" -or -name "*.m" -or -name     "*.swift" ) -print0 
| xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*$" 
| perl -p -e "s/($TAGS)/ warning: $1/"

调整变量以适合。

最新更新