我正在尝试创建NSURLConnection
的子类,其中我有一个额外的属性(在本例中为“connectionName”),以帮助我区分两个不同的连接。
我创建了子类,将其命名为CustomURLConnection
,并为其赋予了属性“connectionName”。
然后在我的文件ImagesViewController.m(这是一个UICollectionView
)我导入标题CustomURLConnection
并尝试给连接一个名称并在之后检索它,但它不起作用,因为一旦我进入此集合视图,应用程序崩溃并给我以下错误:
- [NSURLConnection setConnectionName:]:无法识别的选择器发送到实例0x1090a40f0
以下是一些代码:(如果你愿意,这里是CLEARER IMAGE)
CustomURLConnection.h
#import <Foundation/Foundation.h>
@interface CustomURLConnection : NSURLConnection
@property (strong, nonatomic) NSString *connectionName;
@end
ImagesViewController.h
#import <UIKit/UIKit.h>
@interface ImagesViewController : UICollectionViewController<NSURLConnectionDelegate>
@property (strong, nonatomic) UIImageView *imageView;
@end
ImagesViewController.m
...
#import "CustomURLConnection.h"
@interface ImagesViewController (){
NSArray *contentStrings;
NSMutableData *contentData; // Holds data from the initial load
NSMutableData *contentImageData; // Holds data when the user scrolls up/down in the collection view
}
@end
...
-(void)loadInitialData{ // Loads data from page
NSString *hostStr = @"http://www.website.com/example";
NSURL *dataURL = [NSURL URLWithString:hostStr];
NSURLRequest *request = [NSURLRequest requestWithURL:dataURL];
CustomURLConnection *connectionData = (CustomURLConnection *)[NSURLConnection connectionWithRequest:request delegate:self]; // Make connection
connectionData.connectionName = @"InitialData"; // Give it a name
}
...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
// Do some stuff
NSString *hostStr = @"http://www.website.com/example2";
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[imageCell addSubview:_imageView]; // Adds an image view to each collection cell
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:hostStr]];
CustomURLConnection *connectionImg = (CustomURLConnection *)[NSURLConnection connectionWithRequest:request delegate:self]; // Make connection
connectionImg.connectionName = @"ImageData"; // Give it a different name than before
// Do some stuff
return imageCell;
}
...
// Here are the main methods for the connections
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
if([((CustomURLConnection *)connection).connectionName isEqualToString:@"InitialData"]){
contentData = [[NSMutableData alloc] init];
}
else{
contentImageData = [[NSMutableData alloc] init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if([((CustomURLConnection *)connection).connectionName isEqualToString:@"InitialData"]){
[contentData appendData:data];
}
else{
[contentImageData appendData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if([((CustomURLConnection *)connection).connectionName isEqualToString:@"InitialData"]){
// Do some stuff
}
else{
UIImage *image = [[UIImage alloc] initWithData:contentImageData];
_imageView.image = image;
}
}
我错过了什么吗?我之前多次遇到过这个错误,但原因从来都不一样,这次我似乎无法自己找到解决方案。
希望你能看到我做错了什么并帮助我:)
感谢。
编辑:事实证明有更好的方法来实现我的目标,have a look here
再次感谢大家的帮助:)
答案 0 :(得分:3)
CustomURLConnection *connectionImg = (CustomURLConnection *)[NSURLConnection connectionWithRequest:request delegate:self]; // Make connection
创建一个NSURLConnection
对象。转换为CustomURLConnection
不会改变
这个对象的类。用
CustomURLConnection *connectionImg = [CustomURLConnection connectionWithRequest:request delegate:self]; // Make connection
创建子类的实例。
答案 1 :(得分:2)
在您的委托方法中,通过CustomURLConnection更改NSURLConnection,例如:
- (void)connection:(CustomURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
当你创建它时,只需:
CustomURLConnection *connectionImg = [[CustomURLConnection alloc] initWithRequest:request delegate:self];
connectionImg.connectionName = @"ImageData"; // Give it a different name than before
答案 2 :(得分:1)
在这一行:
CustomURLConnection *connectionData = (CustomURLConnection *)[NSURLConnection connectionWithRequest:request delegate:self];
您正在创建NSURLConnection
的实例,而不是CustomURLConnection
。因此,当您将结果转换为CustomURLConnection *
时,您对编译器撒谎。
稍后,在运行时,当您尝试使用CustomURLConnection
的功能时,您会收到异常,因为您的connection
是错误的类类型,并且没有实现该方法。
您需要实例化CustomURLConnection
,而不是NSURLConnection
。
答案 3 :(得分:0)
在此处添加其他好的答案,您的CustomURLConnection
类应覆盖+connectionWithRequest:delegate:
以返回CustomURLConnection
的实例,如下所示:
+(CustomURLConnection*)connectionWithRequest:(NSURLRequest*)request delegate:(id)delegate
{
return [[CustomURLConnection alloc] initWithRequest:request delegate:delegate];
}
这可以让你使用你拥有的相同风格:
CustomURLConnection *connectionData = [CustomURLConnection connectionWithRequest:request delegate:self]; // Make connection
更重要的是,您的代码的用户(很可能是未来的您)可能会认为向+connectionWithRequest:delegate:
发送CustomURLConnection
将返回CustomURLConnection
的实例。如果没有覆盖,他们将获得NSURLConnection
的实例,而这是一个难以发现的错误。