Swift closure syntax using Shark Food Mute Switch?

372 views Asked by At

I am having trouble with swift closure syntax. I am trying to check the mute switch using Sharkfood which you can see here: http://sharkfood.com/content/Developers/content/Sound%20Switch/

The Block I'm trying to call is shown below.

typedef void(^SharkfoodMuteSwitchDetectorBlock)(BOOL silent);

This is how I'm trying to call it but it isn't working. I've tried a ton of different ways and I know I'm missing something little since I'm new to swift. The error I'm getting is:

'(Bool) -> Void' is not convertible to 'Bool'

On the first line of this code:

muteDetector.silentNotify({
    (silent: Bool) -> Void in
    println("this")
    println("worked")
})

Any help would be greatly appreciated.

1

There are 1 answers

2
Antonio On

Never used that library, but looking at the documentation linked in your question I notice that silentNotify is a property:

@property (nonatomic,copy) SharkfoodMuteSwitchDetectorBlock silentNotify;

containing the block, so the error stating that a BOOL is expected makes sense.

Instead with your code you are probably trying to call this method:

-(void)setSilentNotify:(SharkfoodMuteSwitchDetectorBlock)silentNotify{
    _silentNotify = [silentNotify copy];
    self.forceEmit = YES;
}

I don't know which of the 2 you are attempting to do - if you want to call the block, then you have to just provide a bool:

muteDetector.silentNotify(true)

if instead you want to register a new block (closure), then you have to use:

muteDetector.setSilentNotify({
    (silent: Bool) -> Void in
    println("this")
    println("worked")
})