Reputation: 23
How would I call this using Swift? Can't seem to get the right syntax!
@property (nonatomic, copy) void(^testBlock)(UIImage *image);
So far I've got:
imag.testBlock({(image:UIImage)->Void in
println("test")
})
But it's giving me an error:
(UIImage) -> Void is not convertible to UIImage
Upvotes: 0
Views: 145
Reputation: 56352
Your Objective-C property is a block that takes an UIImage
parameter. When you do
imag.testBlock({(image:UIImage)->Void in
println("test")
})
You are calling the block and passing in another block, which doesn't make sense for your situation.
All you need is imag.testBlock(UIImage(named: "some_image"))
Upvotes: 0
Reputation: 70165
Your testBlock
is an Objective-C property bound to a function. So, in your problem code, are you assigning testBlock
or are you calling the function bound to testBlock
?
imag.testBlock = { (image:UIImage) -> Void in
println ("test")
}
then to use:
imag.testBlock (/* call with some image */)
Upvotes: 1
Reputation: 488
When defining a closure: Closures are typically enclosed in curly braces { } and are defined by a function type () -> (), where -> separates the arguments and the return type, followed by the in keyword which separates the closure header from its body.
{ (params) -> returnType in
statements
}
See: http://fuckingswiftblocksyntax.com/
So in your case you shouldn't use Void as returnType, when you want to return an UIImage
imag.testBlock( { (image: UIImage) -> UIImage in
return UIImage(....)
}
Upvotes: 0