Search code examples
objective-ciosobjective-c-blockssemantics

Objective C - The meaning of this Method and line of code?


First,

 + (void)allOpenRequestsWithBlock:(void (^)(NSArray *requests, NSError *error))block 

Can someone give an in depth explanation what the semantics of this method is? What is the plus sign vs minus.
Can someone explain (void (^)(NSArray *requests, NSError *error))block? It's a void block that retains requests and errors?

This line of code:

 [[AFChangeRequestAPIClient sharedClient] getPath:@"example.json" parameters:[NSDictionary      dictionaryWithObject:@"false" forKey:@"include_entities"] success:^(AFHTTPRequestOperation *operation, id JSON) 

Is this calling the get Path Method that has parameters above? THe Dictionary is passed with just 1 key/value of include_entities/false? What does the success block part mean? Is it the return value?

I am basically trying to understand the AFNetworking framework and it seems to use syntax that I am not familiar with.

Thanks! Alan


Solution

  • This is a very vast question. You shouldn't expect to learn a language simply by example, and should read the Introduction to Objective-C Language in Apple's documentation first before going further. By asking the question you seem to try to understand everything at the same time whereas you don't seem to know the basics of the language itself, and this is not the good approach to learn a language.

    To answer your question anyway:

    • The + sign means it is a class method. The - sign means it is an instance method.
    • The semantic of a method is - (returntype)methodName:(type1)param1 nameContinuation:(type2)param2 ; where the - here thus means it is an instance method, the return type is returntype, the method has the name methodName:nameContinuation: and has two parameters, one of type type1 identified by the variable param1 and one of the type type2 identified with the variable param2
    • In the + (void)allOpenRequestsWithBlock:(void (^)(NSArray *requests, NSError *error))block synthax, then, void(^)(NSArray* requests, NSError* error) is the type of the first parameter. This type describes an Objective-C block, which is something quite similar to a function pointer but that allows closure (and variable capture). For more info about blocks, again, read the dedicated Apple documentation.

    For the second question, the method call is not complete and miss the block body.

    [[AFChangeRequestAPIClient sharedClient] getPath:@"example.json"
                                          parameters:[NSDictionary dictionaryWithObject:@"false" forKey:@"include_entities"]
                                             success:^(AFHTTPRequestOperation *operation, id JSON)
     {
       /* block body here */
     }];
    

    This calls the method getPath:parameters:success: on the object returned by [AFChangeRequestAPIClient sharedClient] (so this is not AT ALL the same method as above). This method is passed three parameters, the first one being @"example.json", the second one being an NSDictionary containing only one key/value pair, and the last one being a Objetive-C block. This block takes two parameters of type AFHTTPRequestOperation and id respectively.

    Again, it really seems that you try to learn stuff too fast and use advanced stuffs (like blocks and their syntax) before understanding the basics (like instance vs. class methods, the basic language syntax of a method, etc). Take your time to learn stuff, you don't build a house in one day, and more important you build the foundation before the roof ;)