-1

I am learning objective c and swift. i didn't get what is difference between block and method or function in Objective c or in Swift.

int mutiplier=10;

int (^myBlock)(void)=^{

    return 10 *3;
};
NSLog(@"%d",myBlock());

Or can write method/function like this

-(int)function:(int)num{

    return num* 10;
}

Blockquote

vaibhav
  • 4,038
  • 1
  • 21
  • 51
Mujahed Ansari
  • 419
  • 4
  • 13
  • have a look this ans ..https://stackoverflow.com/a/39611258/4003548. – vaibhav Jun 01 '17 at 12:31
  • It is my narrow understanding of the topic, but, I think, methods and blocks are, conceptually, very similar. Other languages have concepts like "lambda-functions" or "closures", which are very similar too. I guess, one way to think about blocks is as nameless functions, which, in addition to being nameless, retain the local scope. – Baglan Jun 01 '17 at 13:13

2 Answers2

1

Short and simple:

  1. Block of code - just a block of code. You can declare it, define types of blocks(then make an instances), call blocks one by one, etc. Blocks can take parameters, can return something, It`s quite convenient to use them along with a Grand Central Dispatch. Blocks can declared right in the middle of the code, as an instance variable or as a property. They can also be passed as an arguments to a method/function call. After block has done its work you can call the 'completion' part to run some specific code, which is convenient in some cases. In a swift language similar to blocks (but not equal) thing is a closure. Would like to add that there is a block-based enumeration approach available in Objective-c which is almost as fast as Fast enumeration. Would recommend fast enumeration for most cases, but sometimes (rare) block enumeration is better. Other loops usually is not as fast as this two ones. One more important thing we should keep in mind is that the blocks are Objective-C objects while functions and methods are not. Blocks can capture values from the variables from the enclosing scope while to get the same values inside of a function/method you need to pass these variables as an arguments. Using blocks you can even change these variables using f.e.

    __block int anInteger = 123; 
    

    syntax before the block call. Keep in mind you avoid strong reference to a self when capturing it inside of a block to avoid retain cycles. Use weakSelf in that case. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

  2. Function should have name, return something or be void.

  3. Method is a function of a class.

Alexander
  • 2,803
  • 5
  • 13
  • 21
0

Blocks are a way of wrapping up a piece of code and effectively storing it for later use. A block is commonly used in place of a call back function. Newer APIs in the iPhone SDK use blocks this way. The API will take a "block" of code that it will run on completion. And Blocks are presented in swift as a Closure.

In swift it's little tough syntax to learn closure. but trust me it's very handy way when you start to use it.

It saves you having to create your own threads and maintain the state of each thread, manage locks, setup autorelease pools etc.

Also, when used with the Grand Central Dispatch (GCD) API blocks can be run on queues and entire portions of code can be made to run asynchronously with very little effort, but still keeping the robustness that is required for multithreaded code.

Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40