0

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.

Zen
  • 3,047
  • 1
  • 20
  • 18
  • I think a little bit more code would be needed to see what's going on. – Michał Kreft Nov 30 '12 at 12:50
  • Actually I'm going to parse an RSS feed link from the URL. So the url contains the URL link. – Zen Nov 30 '12 at 13:00
  • @Zen Seconding what @ride.inc said, but I'll elaborate: Even though `responseData` is *supposed* to be a `NSMutableData`, your compiler is telling you it's actually a dictionary. Did you declare `responseData` as a `NSMutableDictionary`, perchance? Is there some place where `responseData` actually wound up storing a reference to a dictionary? – Extra Savoir-Faire Nov 30 '12 at 14:26
  • I think @trudyscousin is right, I second his opinion, that is why I think you need to show us the whole class and a header file, so we can identify the issue. – Michał Kreft Nov 30 '12 at 16:28
  • @ride.inc Thanks for looking into this matter.I have updated the question with the header file code. reponseData has a property with strong and its an NSMutableData instance. Still compiler is getting it as NSMutableDictionary is somewhat confusing to a starter like me. – Zen Dec 01 '12 at 07:31
  • @trudyscousin same as i mentioned in comment above as you suggested. – Zen Dec 01 '12 at 07:32
  • Are you using automatic reference counting or retain–release? – paulmelnikow Dec 02 '12 at 06:17

2 Answers2

3

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..

Wade Tregaskis
  • 1,996
  • 11
  • 15
  • Yeah it helped. Changing an ARC project into a Non ARC one, just blacked out of this. Called an instance variable directly. Thanks a lot. Really. – Zen Dec 03 '12 at 06:14
0

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).

Michał Kreft
  • 548
  • 3
  • 16