1

I am new on Swift and I am trying to use closure for getting results back from viewcontroller

Here is the example

class MD5Calculator {

    static func imageChecksum(imageArray: [UIImage], onCalculated: @escaping () -> [String]){

        DispatchQueue.global(qos: .userInitiated).async {

            var array: [String] = []

            for chosenImage in imageArray {
                if let jpegData = UIImageJPEGRepresentation(chosenImage, 80) {
                    let checksum = jpegData.md5()
                    let chsum = checksum.toHexString()
                    array.append(chsum)
                }
            }

            DispatchQueue.main.async {
                //return array
            }
        }

    }
}

I want md5 calculation do in background and when its done pass it to viewController.

I created closure but I don't know how to return it. my question is:

  1. How to pass string array with closure

  2. How to call ImageChecksum in view controller

3 Answers 3

1

Your usage of closure is not effective. The array you are about to send back should be the parameter of the closure not the returning type.

This should work

class MD5Calculator {

    static func imageChecksum(imageArray: [UIImage], onCalculated: @escaping ([String]) -> ()){

        DispatchQueue.global(qos: .userInitiated).async {

            var array: [String] = []

            for chosenImage in imageArray {
                if let jpegData = UIImageJPEGRepresentation(chosenImage, 80) {
                    let checksum = jpegData.md5()
                    let chsum = checksum.toHexString()
                    array.append(chsum)
                }
            }

            DispatchQueue.main.async {
                //return array
                onCalculated(array)
            }
        }

    }
}

EDIT: Use it in your ViewController like this

MD5Calculator.imageChecksum(imageArray: [imageArray]) { array in
    // 'array' is your returned result
}
Sign up to request clarification or add additional context in comments.

1 Comment

how do I call on viewcontroller ?
0

You should send your result array as a parameter of closure, not as return value.

  1. How to pass string array with closure?

    class MD5Calculator {
    
    static func imageChecksum(imageArray: [UIImage], onCalculated: @escaping (_ checksum: [String]) ->()){
    
        DispatchQueue.global(qos: .userInitiated).async {
    
            var array: [String] = []
    
            for chosenImage in imageArray {
                if let jpegData = UIImageJPEGRepresentation(chosenImage, 80) {
                    let checksum = jpegData.md5()
                    let chsum = checksum.toHexString()
                    array.append(chsum)
                }
            }
    
            DispatchQueue.main.async {
                //return array
                onCalculated(array)
            }
        }
    
    }
    }
    

2.How to call ImageChecksum in view controller

MD5Calculator.imageChecksum(imageArray: []) { (checksum) in

}

Comments

0

The main problem is, that you have defined closure incorrectly. Basically, the idea is, that you should define a closure with a return value, when you want to get the value inside the function, not the outside. The usage (in this case) would be something like: let someConstant = onCalculated(). This is the way you get the value inside the function.

In your case (you want to return a value from function), the closure should be defined something like: onCalculated: @escaping (_ someArray: [String]) -> Void. in this case, you would just return the value from the function: onCalculated(someArray)

The usage:

MD5Calculator.imageChecksum(
    imageArray: inputImageArray, 
    onCalculated: { someArray in
        // enter code here
    }
)

P.S. Consider to use optional unwrap for closures!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.