2

I've inferred what a lot of things DO in Objective-C, and I've gone through several tutorials that simply talk about the data types, but I haven't run across anything that simply explains the syntax.

For starters, what does this mean? What it does is start a thread and get data returned from a server:

- (void)apiCall:(void (^)(NSMutableArray *list))block {

Does something in that function header tell me that it is asynchronous? is that what block means?

jscs
  • 63,694
  • 13
  • 151
  • 195
CQM
  • 42,592
  • 75
  • 224
  • 366
  • 5
    This is three separate questions. The second two have already been answered several times: [What do the plus an minus signs mean in ObjC?](http://stackoverflow.com/questions/2097294/) [What does caret mean in ObjC?](http://stackoverflow.com/questions/1912023/) – jscs May 08 '12 at 20:23
  • The first question is actually pretty interesting, though. Also, this is as close as we get to having a language spec: [The Objective-C Programming Language](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/). (In which you'll find descriptions of the meaning of `+` and `-` before method names and of `^`.) – jscs May 08 '12 at 20:26

3 Answers3

4
  1. No, block doesn't mean asynchronous, a block in Obj-C is just a bit of code that can be passed as an argument to a method.
  2. methods that start with - are instance methods and those that start with + are class methods.
  3. ^ is a syntactic marker to denote a block.
geraldWilliam
  • 4,123
  • 1
  • 23
  • 35
2

For your first question: you would have to look at the API documentation to find out if it is asynchronous.

For more information about blocks in general, see here:

Apple Blocks Programming Guide

GorillaPatch
  • 5,007
  • 1
  • 39
  • 56
2

Let's start with your second bullet: Class methods are declared with +, instance methods are declared with -.

The first and third are related, the parameter named block is a code block, it's a piece of code intended to be run later. Given the name of this method apiCall, I suggest this being the method run after the call is done.

It would we natural to suspect that this method will do some work on another thread and then invoke the block you supplied, but for this you'd need to check the documentation or the code.

The signature: (void (^)(NSMutableArray* list)) block describes a code block with a void return type and a NSMutableArray* list as only parameter.

An example usage of the block parameter would be:

void (^apiCallCallback)(NSMutableArray*) = ^(NSMutableArray* list) {
    NSLog(@"The API returned %d items in a list", [list length]);
}

[someApiInstance apiCall:apiCallCallback];

After the API instance is done doing whatever it is suppose to do, you'll see that the log statement is printed.

Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30
  • 1
    [They're not static methods, they're class methods](http://stackoverflow.com/questions/8089186/objective-c-difference-between-class-method-and-static-method/8089623#8089623). – jscs May 08 '12 at 21:24