gpt4 book ai didi

ios - 子类化 NSURLConnection 给出错误 : unrecognized selector sent to instance

转载 作者:行者123 更新时间:2023-11-28 18:19:37 25 4
gpt4 key购买 nike

我正在尝试创建 NSURLConnection 的子类,其中我有一个附加属性(在本例中为“connectionName”)以帮助我区分 2 个不同的连接。

我创建了子类,将其命名为 CustomURLConnection 并赋予其属性“connectionName”。

然后在我的文件 ImagesViewController.m(这是一个 UICollectionView)中,我导入 header CustomURLConnection 并尝试为连接命名并在之后检索它,但是它不起作用,一旦我进入此 Collection View ,应用程序就会崩溃并出现以下错误:

-[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
再次感谢大家的帮助:)

最佳答案

CustomURLConnection *connectionImg = (CustomURLConnection *)[NSURLConnection connectionWithRequest:request delegate:self]; // Make connection

创建一个 NSURLConnection 对象。转换为 CustomURLConnection 不会改变这个对象的类。将该行替换为

CustomURLConnection *connectionImg = [CustomURLConnection connectionWithRequest:request delegate:self]; // Make connection

创建子类的实例。

关于ios - 子类化 NSURLConnection 给出错误 : unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com