2011-11-30 115 views
0

我想覆盖NSURLConnection委托,但我不知道从哪里开始,有人可以给一些更多的信息和一点样本代码?扩展协议来扩展NSURLConnection的委托方法

我想扩展connectionDidFinishLoading委托。并继续寻找如果我回来的JSON字符串得到了一些用户的错误报告。

在我看来,扩展委托的最好方法。甚至有可能?

回答

1

扩展协议将允许您添加方法。不过,如果你添加更多的方法来<NSURLConnectionDelegate>,不会意味着NSURLConnection旨意使用它们:)

为什么不把你的错误校验码在connectionDidFinishLoading方法即

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // Check your json here 
} 
+0

我想在我的connectionDidFinishLoading方法中实现一个标准错误检查。我不想在我的所有控制器中检查它。我会为此增加一倍的代码,我认为这将是扩展connectionDidFinishLoading协议的最佳选择。 – mariusLAN

+0

然后我会编写一个创建NSURLConnection的通用对象,将它自己作为委托,完成所有的数据收集和错误检查。你所有的控制器都会使用这个对象来代替NSURLConnection。你的控制器将是你的新对象的委托,而不是NSURLConnection的委托。 (你将不得不编写自己的协议,但这不是太棘手)。 – deanWombourne

+0

是的,我是这样描述它的!谢谢:) – mariusLAN

0

在您的.h文件中: -

@interface YourViewController : UIViewController<YourDelegate>{ 

} 
@end 

在您.m文件: -

实施方法: -

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [connection release]; 
    self.responseData = nil; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    [connection release]; 


} 

我认为this link可以帮助你... :)

+0

这就是我想要的究竟。我知道如何创建一个URL Conenction并获取它的数据。我想通常这样做。 – mariusLAN

1

如果我已经正确理解你想要实现的是以下内容:

  • 创建一个NSURLConnection并将其配置为获取JSON文件。
  • 连接完成后,检查收到的文件是否满意。
  • 如果文件不满意,则提醒用户。

如果我的理解是正确的,那么你不需要扩展NSURLConnectionDelegate协议。你所需要做的就是实现NSURLConnectionDelegate。委托模式允许更改类的行为。 (在其他语言/框架中,您解释的行为将通过子类化来实现,URL连接类将被划分子类和方法并被覆盖以改变行为。)读取Cocoa Design Patterns值得您一段时间。

创建对象的类是其代表的常见现象。以下代码显示了创建连接和随附的委托方法实现。

@interface SOViewController : UIViewController <NSURLConnectionDelegate> //this simply tells the compiler that SOViewController implements the NSURLConnectionDelegate protocol. If you excluded you will get a compiler warning but the code will behave correctly. You should include it. 
//... 
@property(readwrite, nonatomic, retain) NSURLConnection *connection; 
@property(readwrite, nonatomic, retain) NSMutableData *data; 
@end 

@implementation SOViewController 

//... 

-(void)setupJSONFetch:(NSURL *)url 
{ 
    NSURLRequest *request = [NSURLRequest requestWithURL: url]; //create a request 
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; //create a connection and set self as the delegate 
    self.connection = connection; //keep a reference to the connection 
    self.data = [NSMutableData data]; //create an object to store the downloaded data 
    [connection start]; //go! 
} 


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    //store the downloaded data 
    [self.data appendData: data]; 
} 

- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL 
{ 
    //Check self.data is as expected 
} 

//... 

@end 

另外值得一提的是,NSURLConnection委托方法得到了在IOS 5改组不

+0

对我来说,唯一的办法是用我自己的代表编写一个自己的类。我认为这是我可以选择的最佳方式 – mariusLAN