Objective C "callback"

Objective C "callback"

我是 Objective C 的新人。现在我正在开发我的新应用程序,我对回调函数有一些疑问。

所以这是我的场景:

我有一个名为 Person 的 NSObject class,它代表(显然)一个人。 这个 class 有一些参数,如:人名、人名等。 在 Person.m 中,我有一个函数调用我的休息 API,检索我需要的所有数据,解析 JSON 响应,创建 Person 对象并将其插入 NSMutableArray。 这是我的 Person.h 的示例:

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property(nonatomic, assign) id delegate;


@property NSString * name;
@property NSString * secondName;


-(void)getPersons;

@end

和Person.m文件:

#import "Person.h"
#import <AFNetworking.h>
#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

还有我的 ViewController class。我正在从我的 ViewController class 调用我的 Person class 以获取一个数组中的所有 Person 对象。 这是 ViewController.h 的示例:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    /**
     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

和ViewController.m:

#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

因此,其工作方式如下:

  1. 从 ViewController.m 我在 Person.m
  2. 中调用我的 getPersons 函数
  3. 收到 JSON 响应后,我正在解析它并创建包含 Person 对象的数组
  4. 在ViewController.m
  5. 中调用"callback"函数

所有这一切都有效,因为我使用委托,但实际上,我不知道什么是委托... 我认为有另一种更专业的方法来实施像我这样的解决方案。 (也许是块或类似的东西?)

这是我通常做的。 在 Person.h

+ (void)personsWithCompletion:(void (^)(NSArray *result, NSError *error))completion;

在你的 Person.m

+ (void)personsWithCompletion:(void (^)(NSArray *result, NSError *error))completion{

    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];

//here you call the completion handler
if(completion) completion(allPersosn, error);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

并且您从 VC

使用它
[Person personsWithCompletion:^(NSArray *result, NSError *error) {
        if (error == nil) {
            //do stuff
        }
    }];

希望对您有所帮助。