Xcode 9 releasing support for searching using RegEx opens to the door to using this powerful tool to speed up development workflows.
Today I will be sharing two handy Swift RegEx commands I came up with.
The first is to find non-typed variables. For example.
var myVariableA = 1 // match
var myVariableB: Int = 2 // non match
var myVariableC // match
var myVariableD: String // non match
The RegEx search for this is below. It will work on both var
and let
.
(let|var)( )(\w+)([[:blank:]]*=[[:blank:]]*)
The second cool RegEx search I created was to find all .init
's and remove them.
^(?!(\s*super\.init))((\s*)(.*)(\.init)(.*))
Then if you choose to replace with the following it will automatically remove all .init
's.
$3$4$6
For example in Xcode.
That will replace the following.
var myObject: Person = Person.init(firstName: "Bob", lastName: "Miller")
to
var myObject: Person = Person(firstName: "Bob", lastName: "Miller")
Some developers might not find this useful as I'm sure there are people who like to be specific and define .init
to make it more clear what the code is doing. But personally I find this shortcut pretty handy.
Let me know if you have any other cool Swift RegEx searches or any suggestions for how to improve the commands above.