timpone
timpone

Reputation: 19969

what does this method signature mean

I'm looking at the source code for AFNetworking (and am noob at Objective-C and AFNetworking) and am trying to understand some inherited code. It looks like they are blocks that return nothing, pass in an AFHTTPRequestOperation and either the response or the NSError. Is this a block definition? What does the success / failure at the end signify? I'm thinking it's something like success:MyParam(NSString *)my_param. Just like a single sentence would suffice.

- (void)getPath:(NSString *)path
     parameters:(NSDictionary *)parameters
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{

Any help is appreciated. Thx in advance

Upvotes: 1

Views: 104

Answers (2)

ZhangChn
ZhangChn

Reputation: 3184

The success and failure parameters are blocks, an extension to the C programming language by Apple. A block is very similar to an anonymous function pointer.

It plays the role of a drop-in call-back function in this scenario. If the path were successfully got, success call-back would be invoked, or the failure call-back would be invoked.

The signature specified what parameters these call-back blocks should accept, respectively. The actual values of these parameters would be provided by this AFNetworking call.

Upvotes: 3

Metabble
Metabble

Reputation: 11841

Yes, those are blocks. The first thing you see, the success/failure at the beginning, is part of the method's name and is what you would put before that argument when you pass it to the method. The void indicates the block returns no value. The ^ screams "I'm a block!" at you. The block takes two arguments; a AFHTTPRequestOperation * which is known as operation inside the block and an id which is known as responseObject inside the block. The success/failure at the end is the name of the whole block argument that is used to reference it inside the method receiving it.

Upvotes: 2

Related Questions