Huwell
Huwell

Reputation: 79

What's mean in this SWIFT statement?

/// Image handler, used when setting up an image using some sort of process.
open var imageHandler: ((UIImageView)->Bool)?
/// The image view.
fileprivate var imageView: UIImageView!
...
let showImage = imageHandler?(imageView) ?? false

What's mean about this statement

let showImage = imageHandler?(imageView) ?? false

So many ? make me crazy.

Upvotes: 2

Views: 155

Answers (5)

Alex Pelletier
Alex Pelletier

Reputation: 5123

I might suggestion a default implementation for imageHandler, especally if you are going to use that function more then once. Something like this is cleaner IMHO:

// Image handler, used when setting up an image using some sort of process.
var imageHandler: ((UIImageView)->Bool) = { _ in return false }

// The image view.
fileprivate var imageView: UIImageView!

// Checking if should show image
let showImage = imageHandler(imageView)

You can even change { _ in return false } to { _ in false }, but I'm not sure if that is worth the lack of readability for a little shorter of a line.

If you can't make your imageHandler an optional you could unwrap it step by step as suggested by other people. But I don't think your initial showImage declaration is to gross. Its going to be easier to read then 4 lines of unwrapping.

Upvotes: 0

Raj Sharma
Raj Sharma

Reputation: 1137

enter image description here

“imageHandler?(imageView)” will return some value and that value will be assigned to showImage. If it returns nil than false will get assign to showImage.

Upvotes: 4

Leandro Rocha
Leandro Rocha

Reputation: 31

If the optional imageHandler is set, the call to it succeeds and the value returned is unwrapped and assigned to showImage. Otherwise, showImage is set to false.

Optional Chaining: https://docs.swift.org/swift-book/LanguageGuide/OptionalChaining.html

Nil-coalescing operator: https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html

Upvotes: 3

Ratul Sharker
Ratul Sharker

Reputation: 8013

Explanation:

let showImage = imageHandler?(imageView) ?? false

First thing is first, imageHandler is a code block, which is declared to take an UIImageView and the whole imageHandler could be just nil.

So the first ? in the statement

imageHandler?(imageView)

Just fine an simple optional chaining.

Secondly the imageHandler returns a BOOL. But the imageHandler itself is optional. So incase of imageHandler itself is nil, what should be the value to be assigned in showImage. So this code decide that false using nil-coalescing.

You could just use optional unwrapping as follows

var showImage = false
if let result = imageHandler?(imageView) {
    showImage = result
}

So i think you got everything you want to know.

Upvotes: 3

Abu Ul Hassan
Abu Ul Hassan

Reputation: 1396

Its an Optional chaining Statemen this statement is equal to following...

if imageView != nil {
   showImage = true
}
else {
    showImage = false
}

In optional chaining we write it like your example

Upvotes: 0

Related Questions