【发布时间】:2015-11-01 11:54:53
【问题描述】:
我是 Objective C 的新手。现在我正在开发我的新应用程序,我对回调函数有一些疑问。
所以这是我的场景:
我有一个名为 Person 的 NSObject 类,它(显然)代表一个人。 这个类有一些参数,比如:人名、人名等。 在 Person.m 中,我有一个函数可以调用我的 rest 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 类。从我的 ViewController 类中,我调用我的 Person 类以将所有 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
所以,它的工作原理如下:
- 从 ViewController.m 我在 Person.m 中调用我的 getPersons 函数
- 收到 JSON 响应后,我正在解析它并使用 Person 对象创建数组
- 在 ViewController.m 中调用“回调”函数
所有这些都有效,因为我使用委托,但实际上,我不知道什么是委托... 我认为有另一种更专业的方式来实施像我这样的解决方案。 (也许是块或类似的东西?)
【问题讨论】:
-
要了解有关委托和协议的更多信息,您应该阅读以下内容:developer.apple.com/library/ios/documentation/Cocoa/Conceptual/…
-
我不知道为什么这么多人喜欢 IOS 中的委托。当然,有时您必须使用它,但是当为具有成千上万行代码的应用程序编写代码时,委托可以将您的代码库变成依赖项的噩梦。当然,当 Apple 在其 UIKit 元素中设置了委托时,使用委托很重要,但您根本不必使用自己的自定义委托。事实上,我使用自定义委托零次编写了超过 50k 行代码的应用程序。我会在这里发布一个答案来帮助你,但是堆栈中有太多喜欢委托的脑死亡开发人员。
-
continuation .. 并且很想对我投反对票,因为他们不知道如何正确子类化并使用高级 setter/getter 和 C 块操作来避免使用委托。 Apple 自己的委托东西很有用,但是当我这么说的时候,我是认真的,如果你从非常强烈的 CLang 角度学习编写 IOS 并坚持这种结构,那么你真的不需要在此之外使用它:不要使用情节提要,以编程方式创建所有视图,将代码封装到每个视图控制器的点,仅包含单独视图的控制代码,所有视图代码都包含在单独的..
-
子类文件,例如,您有 EXViewController.h 和 EXViewController.m,它们使用 EXView.h 和 EXView.m,EXView 使用 EXButtonSubClass.h 和 EXButtonSubClass.m,EXView 也使用 EXCustomTextField.h 和EXCustomTextField.m 等,等等,子类化到一切都在它自己的宇宙中,同时使用从视图控制器 init 方法流向子视图并从那里填写所有信息的自定义 getter 和 setter 传递值。委派是解决此问题的一种廉价方法,但它也是一种非常不雅的解决方案。
-
“委托”只是一个对象,您可以调用它来提供数据或执行某些功能。它提供了数据/函数和使用它的代码之间的隔离,以便它们可以单独开发和维护,从而避免过度的相互依赖。这不是魔术。就像任何编程概念一样,它可能会被过度使用或不当使用。
标签: ios objective-c json object callback