Dan
Dan

Reputation: 485

Declare & implement an iOS method with blocks, but no other parameters

Need help declaring and implementing a method with blocks but no parameters. Sounds simple but I'm missing something because this works:

- (void) RetrieveDevices: (NSInteger)count
         success:(void (^)(NSMutableArray *devices))success
         failure:(void (^)(aylaError *err))failure;

- (void)RetrieveDevices:(NSInteger)count
        success:(void (^)(NSMutableArray *devices))successBlock
        failure:(void (^)(aylaError *err))failureBlock
{

}

And this won't compile as it's expecting a method body:

- (void) RetrieveDevices
             success:(void (^)(NSMutableArray *devices))success
             failure:(void (^)(aylaError *err))failure;

- (void)RetrieveDevices
            success:(void (^)(NSMutableArray *devices))successBlock
            failure:(void (^)(aylaError *err))failureBlock
{

}

Appreciate the help.

Upvotes: 5

Views: 7203

Answers (4)

Guillaume
Guillaume

Reputation: 4361

The problem is not the block syntax, it's the method declaration syntax of the second example. A method with no parameter is declared as:

- (RETURN_TYPE)method_name

and a method with parameters is declared as:

- (RETURN_TYPE)method_name_part1:(PARAMETER_TYPE1)parameter1 name_part2:(PARAMETER_TYPE2)parameter2...

The first example has the correct syntax, with a void return type, and three parameters, the second example has a space after the method name, which is why the compiler expects the body of the method (which he interprets as a no parameter method).

Also note that, by convention, methods names start with a lowercase letter.

Upvotes: 0

Rob
Rob

Reputation: 437392

You could do something like:

- (void) RetrieveDevicesSuccess:(void (^)(NSMutableArray *devices))success
                        failure:(void (^)(aylaError *err))failure;

Upvotes: 0

Andrew Madsen
Andrew Madsen

Reputation: 21373

The problem is the newline and whitespace between "RetrieveDevices" and "success"/"failure". Try this instead:

- (void)RetrieveDevicesOnSuccess:(void (^)(NSMutableArray *devices))successBlock
                       onFailure:(void (^)(aylaError *err))failureBlock
{

}

Upvotes: 1

mikiancom
mikiancom

Reputation: 220

Blocks are parameters. So you want a method signature with two parameters. Try e.g.:

- (void) RetrieveDevicesWithSuccess:(void (^)(NSMutableArray *devices))success
                            failure:(void (^)(aylaError *err))failure;

Upvotes: 12

Related Questions