英文:
How to find all places in a project, where overridden ~= is used?
问题
extension String {
static func ~= (lhs: String, rhs: String) -> Bool {
guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
let range = NSRange(location: 0, length: lhs.utf16.count)
return regex.firstMatch(in: lhs, options: [], range: range) != nil
}
}
我找不到一种方法来查找项目中使用了这个重写函数的所有位置。
在这种情况下,Cmd+Ctrl+Shift+H
不起作用。
你可以说:“只需搜索代码库中的~=
符号”。
但~=
也隐式用于评估switch-case
操作符。
用例:
假设我想更改~=
函数的逻辑(我真的想这样做)。
我确切地知道,我还必须修复使用这个函数的地方的代码。
否则,这些switch-case
将不正确地工作(编译器不会显示任何警告)。
附言:
作为解决方案的示例:搜索代码库中的所有switch
出现,并在其中仅查找switch String
/case String
。
这正是我所需要的。
这种方法的唯一问题是:这将花费我几个小时(如果不是几天)🤣
英文:
extension String {
static func ~= (lhs: String, rhs: String) -> Bool {
guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
let range = NSRange(location: 0, length: lhs.utf16.count)
return regex.firstMatch(in: lhs, options: [], range: range) != nil
}
}
I can’t figure out a way to find all places in a project, where this overridden function is used.
Cmd+Ctrl+Shift+H
doesn’t work in this case for some reason.
You can say: “Just search for ~=
symbols in the codebase”.
But ~=
is also implicitly used for evaluation of switch-case
operators.
Use case:
Let’s say I want to change the logic of the ~=
function (and I really do). And I know for sure, that I also have to fix the code in places, where this function is used. So I need to find all the switch-cases
where it is used. Otherwise these switch-cases
will become work incorrectly (and the compiler will not show any warnings).
P. S.
As an example of a solution: to search for all the switch
appearance in the codebase and among them look only for switch String
/case String
. That would be exactly what I need.
The only problem with this approach: it will take me hours (if not days) 🤣
答案1
得分: 1
如果您要弃用覆盖,那么您只需查看编译器警告。
extension String {
@available(*, deprecated, message: "Found it")
static func ~= (lhs: String, rhs: String) -> Bool {
guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
let range = NSRange(location: 0, length: lhs.utf16.count)
return regex.firstMatch(in: lhs, options: [], range: range) != nil
}
}
英文:
What if you were to deprecate the override? Then you just have to look through your compiler warnings.
extension String {
@available(*, deprecated, message: "Found it")
static func ~= (lhs: String, rhs: String) -> Bool {
guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
let range = NSRange(location: 0, length: lhs.utf16.count)
return regex.firstMatch(in: lhs, options: [], range: range) != nil
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论