Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I put common utility functions in iOS 8 + Swift

I have the following method (for instance) that I'd like to be able to use from any ViewController in an iOS + Swift project:

func initializeBlurEffectOnGivenUIView(UIViewToBeBlurred: UIView) {

    let UIViewToBeBlurredHeight = UIViewToBeBlurred.frame.size.height
    let UIViewToBeBlurredWidth = UIViewToBeBlurred.frame.size.width
    let UIViewToBeBlurredX = UIViewToBeBlurred.frame.origin.x
    let UIViewToBeBlurredY = UIViewToBeBlurred.frame.origin.y

    let blurEffect:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)

    let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
    let blurEffectView:UIVisualEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = CGRectMake(UIViewToBeBlurredX, UIViewToBeBlurredY, UIViewToBeBlurredWidth, UIViewToBeBlurredHeight)
    blurEffectView.contentView.addSubview(vibrancyEffectView)

    UIViewToBeBlurred.addSubview(blurEffectView)
}

I have more common helpers such as there that I'd like to make available as well. How do I structure the application to achieve this?

like image 860
John Doe Avatar asked Dec 13 '14 18:12

John Doe


People also ask

How do you call a function in Swift?

To use a function, you “call” that function with its name and pass it input values (known as arguments) that match the types of the function's parameters. A function's arguments must always be provided in the same order as the function's parameter list.

How do you write a utility function?

A utility function that describes a preference for one bundle of goods (Xa) vs another bundle of goods (Xb) is expressed as U(Xa, Xb). Where there are perfect complements, the utility function is written as U(Xa, Xb) = MIN[Xa, Xb], where the smaller of the two is assigned the function's value.

How do you return a function in Swift?

To do this, write a dash then a right angle bracket after your function's parameter list, then tell Swift what kind of data will be returned. Inside your function, you use the return keyword to send a value back if you have one.


2 Answers

If the function is to be a free global, put the function at the top level of any file. (Just don't accidentally repeat yourself and put the same function at the top level of all files.) That, for example, is what I do with the delay utility function I describe here: dispatch_after - GCD in swift?

In the particular case of the example you gave, however, I'd probably put it in an extension on UIView. I'd rewrite it as an instance method, so that instead of blurring another view, we'd start with an existing view and blur self.

like image 103
matt Avatar answered Oct 08 '22 10:10

matt


Use an extension, like this:

extension UIView {
    func initializeBlurEffect() {

        let height = self.frame.size.height
        let width = self.frame.size.width
        let x = self.frame.origin.x
        let y = self.frame.origin.y

        let blurEffect:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)

        let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
        let blurEffectView:UIVisualEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = CGRectMake(x, y, width, height)
        blurEffectView.contentView.addSubview(vibrancyEffectView)

        self.addSubview(blurEffectView)
    }
}

Then call it like this:

myview.initializeBlurEffect()
like image 44
Mike Taverne Avatar answered Oct 08 '22 10:10

Mike Taverne