문제

Trying to create a connection of a request with an URL. An NSMutableData instance (responseData) also gets called with it. When the connection starts receiving response, the setLength:NSUInteger method gets called up on the NSMutableData Instance.

-(void)startDataDownloading
{
    NSURLRequest *_request = [NSURLRequest requestWithURL:self.url];
    if (_request) {
        if (!connecton) {
            connecton = [NSURLConnection connectionWithRequest:_request delegate:self];
            if (connecton) {
                responseData = [NSMutableData data];
                [connecton start];
            }
        }
    }
}

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

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

But somehow it causes a crash with a warning on the setLength call. The error states that

" -[__NSCFDictionary setLength:]: unrecognized selector sent to instance 0x6a8cf70 2012-11-30 18:00:38.948 RSSReader[8997:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setLength:]: unrecognized selector sent to instance 0x6a8cf70' "

Any hint about this would be appreciated.

#import <Foundation/Foundation.h>
#import "DataParser.h"

@protocol DataConnectionDelegate <NSObject>
//protocol methods
@end
@interface UCDataConnection : NSObject <ModelParser>
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, strong) NSURLConnection *connecton;
@property (strong, nonatomic) NSMutableData *responseData;
@property (nonatomic, assign) id<DataConnectionDelegate> delegate;
-(void)startDataDownloading;
- (id)initWithUrl:(NSURL *)_url andDelegate:(id<DataConnectionDelegate>)_delegate;

That is a part of the header file.Sorry for late response.

도움이 되었습니까?

해결책

Most likely you're not retaining responseData correctly, so it's being released and in your above example you happen to end up getting an NSDictionary allocated in the same place.

If you're using ARC then the code you posted is fine (other than that "responseData" should probably have an underscore prefix, assuming it's an instance variable).

If you're using retain-release, then you need to add a call to retain when you allocate responseData.

Update: Based on your header file it looks like you're referring to the instance variable directly, and using retain-release. Your best option is to refer to responseData only through the property mechanism - i.e. prefix all its uses with self..

다른 팁

I don't know if this is the answer, but what I see suspicious here is that you have a property

@property (strong, nonatomic) NSMutableData *responseData;

and by default it should be accessed with self. responseData;

if you intend to access private ivar you should by default use _responseData.

Unless you said differently in .m file which I would also like to see, so to be sure what's going on (in case this answer won't help).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top