0

As I understood right we can use function

func saveImage(imageType:imageType, thumb: UIImage, completion: CompletionHandler)

like this:

imageSaver.saveImage(someType, thumb:image) { in
...
}

but actually as from Objective-C I suppose that block is a parameter and maybe should I use it like this:

imageSaver.saveImage(someType, thumb:image, { in
...
})

Also one more that is interesting for me as Objective-C coder it's passing arguments to a function:

so if I want to add some params to savaImage function like this:

func saveImage(someInt:UInt, imageType:imageType, thumb: UIImage, completion: CompletionHandler)

then I should call it with specifying imageType name.

imageSaver.saveImage(10, imageType:someType, thumb:image) { in
...
}

I guess in Swift we have something like skipping definition of function

Objective-C example:

- (void)saveImage:(UIImage *)image withParams:(NSDictionary *)params

So withParams sometimes can be skipped at start of function, as I understood right, which is not quite obvious for me)

2
  • what question are you asking? Commented Apr 18, 2016 at 15:56
  • @PEEJWEEJ my question is it legal to implement block outside () of function, so as I see it's a last parameter of function. in objective c we did not use it like that. Commented Apr 18, 2016 at 16:16

1 Answer 1

2

Yes that's part of the purpose of the distinction between a closure and a function. It's not really defining a block outside the function, so much as some syntactic sugar to improve readability.

I would suggest reading through this: Closure Documentation

Sign up to request clarification or add additional context in comments.

2 Comments

ok got it, but it means that it will work only in case if the closure is a last param in the function. right?
I believe that's correct. It also doesn't work from within "if let" or "guard let" statements

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.