Parth Adroja Parth Adroja - 29 days ago 11
Swift Question

Regular Expression to replace CGRectMake

As CGRectMake is not available in swift 3.0 and migrator did not convert it i need to manually replace it and it is used more than 300 times in my code so can anyone help me in this to develop regular expression so i can find and replace code. What i want to do is to convert

CGRectMake(a,a,a,a) here a is some value.


CGRect(x: a, y: a, w: a, h: a)


EDIT - With Regex:


and replace with

CGRect\(x:\1, y:\2, width:\3, height:\4\)

Using regex we can not get the value of x,y,width,height dynamically.

So Alternative solution to above is,

  • Replace all the CGRectMake( with appdelegateobject.CGRectMakeWrapper("
  • At the end add " only.

Means in your case you will replace CGRectMake(a,a,a,a) to appdelegateobject.CGRectMakeWrapper("a,a,a,a"

where appdelegateobject is the appdelegate shared instance object where you will define CGRectMakeWrapper function having string parameter as shown below :

func CGRectMakeWrapper(str: String) -> CGRect {

    var rect = CGRectZero

    if(str.characters.count > 0)
        var arr = str.componentsSeparatedByString(",")

        if(arr.count == 4)
            rect = CGRect(x: CGFloat((arr[0] as NSString).doubleValue), y: CGFloat((arr[1] as NSString).doubleValue), width: CGFloat((arr[2] as NSString).doubleValue), height: CGFloat((arr[3] as NSString).doubleValue))


    return rect

var rect = "10.0,10.0,100,100" //String

var rect1 = CGRectMakeWrapper(rect)  //CGRect

I have shown sample string as rect and passed to CGRectMakeWrapper function which will return a rect. You can define this CGRectMakeWrapper function in common class which is accessible to all classes(e.g Appdelegate file).