Benzino
Benzino

Reputation: 577

'unrecognized selector sent to instance'?

I'm new to Objective C and having trouble understanding why I am getting this error. I've checked other similar questions, but haven't been able to resolve the issue.

The error is "-[NSConcreteMutableData base64Decoded]: unrecognized selector sent to instance 0x6e15610"

Here is a snippet of the problem code, where the call to base64Decoded is causing the crash.

#import "DDData.h"

- (NSString *)decodeBase64:(NSString *)input
{
    NSData* dataDecoded = [[input dataUsingEncoding:NSUTF8StringEncoding] base64Decoded];    
    return [NSString stringWithUTF8String:[dataDecoded bytes]];
}

And in DDData.h:

#import <Foundation/Foundation.h>

@interface NSData (DDData)

- (NSData *)base64Decoded;

@end

and DDData.m:

@implementation NSData (DDData)

- (NSData *)base64Decoded
{
     // Excluding function code, as it never gets to here
}

@end

Just a note that the Project has ARC enabled. Any ideas as to what might be the issue here? Thanks.

EDIT: I have adjusted the above code to help debug the error:

 NSData* dataDecoded = [input dataUsingEncoding:NSUTF8StringEncoding];
[dataDecoded base64Decoded];

dataDecoded gets a value from dataUsingEncoding, it is not nil when the call to base64Decoded is made. When I step over to the called to base64Decoded, it crashes.

Upvotes: 0

Views: 4828

Answers (4)

Sanyam Bhasin
Sanyam Bhasin

Reputation: 51

SimonH pointed out the solution correctly in one of the sub-comments. I was having the same problem with a custom method i defined in a NSData category. The solution better explained:

  • Make sure the .m file is included in the projects Build Phases->Compile Sources.
  • Right click on the .m file in the project navigator and click "Show file Inspector". Under File Inspector make sure you check the target you are building for otherwise it wont be included and the calling that method will crash.

Upvotes: 2

BornCoder
BornCoder

Reputation: 1066

Follow below debugging steps to resolve it.

  1. Put breakpoints in your code and check step by step where that is breaking.
  2. Also, check if you have added DDData.m source file in your project target properly.
  3. You should also check the object presence before using it. Check below sample code.

    - (NSString *)decodeBase64:(NSString *)input {
         if(input) {
             NSData *utfData =  [input dataUsingEncoding:NSUTF8StringEncoding];
             if(utfDFata) {
                 NSData* dataDecoded = [utfDFata base64Decoded];    
                 return [NSString stringWithUTF8String:[dataDecoded bytes]];
             }
    }
    

Upvotes: 0

Asciiom
Asciiom

Reputation: 9975

You get that kind of message if you try to execute an undefined method on an object. Try it like this:

NSData *dataDecoded = [[input dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];

There is no base64Decoded method as far as I know, but there is base64EncodedString. So when you send the base64Decoded message to your NSData object, it isn't recognized because it's simply not there.

Upvotes: 1

AppHandwerker
AppHandwerker

Reputation: 1788

Insert a break point in your code and step through it and you'll see exactly where it breaks.

You may also want to check that the DDData files are properly included in your project by looking at the target membership of those files, the .m should be ticked.

Upvotes: 2

Related Questions