title: Objective C "callback"
body: <p>i'm new in Objective C. Now i'm developing my new app and i have some questions about callback functions.</p>
  
  <p>So this is my scenario:</p>
  
  <p>I have a NSObject class called Person that represents (obviously) to a person.
  This class have some parameters like: person name, person second name, etc.
  In Person.m i have a function that calls to my rest API, retrieve all data what i need, parse JSON response, create Person objects and insert it to NSMutableArray.
  Here is an example of my Person.h: </p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface Person : NSObject
  @property(nonatomic, assign) id delegate;
  
  
  @property NSString * name;
  @property NSString * secondName;
  
  
  -(void)getPersons;
  
  @end
  </code></pre>
  
  <p>And Person.m file:</p>
  
  <pre><code>#import "Person.h"
  #import &lt;AFNetworking.h&gt;
  #import "ViewController.h"
  
  @implementation Person
  @synthesize delegate;
  
  // Call this function to get all persons
  -(void)getPersons{
  
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      [manager GET:@"www.myApi.com/persons" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSMutableArray *allPersons = [[NSMutableArray alloc]init];
          // Parsing JSON response and getting array with all persons
          allPersons = [self parseaJson:responseObject];
          // Calling my "callback" function in my ViewController.m
          [self.delegate callback:allPersons];
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];
  }
  
  // This function will parse json response and create array with Person objects
  -(NSMutableArray *)parseaJson:(NSDictionary *)jsonResponse{
      NSMutableArray *allPersons = [[NSMutableArray alloc]init];
      NSDictionary *resources = [jsonResponse objectForKey:@"resources"];
  
      for (NSDictionary *jsonObject in resources) {
          Person *personObject = [[Person alloc]init];
          personObject.name = [jsonObject objectForKey:@"name"];
          personObject.secondName = [jsonObject objectForKey:@"secondname"];
  
          [allPersons addObject:personObject];
      }
  
      return allPersons;
  }
  
  @end
  </code></pre>
  
  <p>Also i have my ViewController class. From my ViewController class i'm calling to my Person class to get all Person objects in one array.
  Here is an example of ViewController.h:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface ViewController : UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt;{
      /**
       Array with all Person objects
       */
      NSMutableArray * personsArray;
  }
  
  
  /**
   This function calls when Person.m have finished to retrive all persons from my API
  */
  -(void)callback:(NSMutableArray *)recivedPersons;
  
  @property (nonatomic, strong) IBOutlet UITableView *table;
  
  @end
  </code></pre>
  
  <p>And ViewController.m :</p>
  
  <pre><code>#import "ViewController.h"
  #import "Person.h"
  
  @interface ViewController ()
  @end
  
  @implementation ViewController
  
  - (void)viewDidLoad {
      [super viewDidLoad];
      self.table.delegate = self;
      self.table.dataSource = self;
  
  
      // GETTING ALL PERSONS
      Person * myPersonClass = [[Person alloc]init];
      [myPersonClass getPersons];
      myPersonClass.delegate = self; // This line is very important...
  }
  
  -(void)callback:(NSMutableArray *)recivedPersons{
  
      personsArray = [[NSMutableArray alloc]initWithArray:lineasBus];
      // Reloading my table
      [self.table reloadData];
  
  }
  ...
  @end
  </code></pre>
  
  <p>So, this works as follow:</p>
  
  <ol>
  <li>From ViewController.m i'm calling my getPersons function in Person.m</li>
  <li>After getting JSON response i'm parsing it and create array with Person objects</li>
  <li>Call "callback" function in ViewController.m</li>
  </ol>
  
  <p>All of this works because I use delegate but really, i don't know what is delegate...
  I think that there is an alternative and more professional way to implement solutions like in my case. 
  (Maybe blocks or something like this ?)</p>