Farn Wang
Farn Wang

Reputation: 159

xcode selector not found

I have written the following statement :

int  kk = [MartialArtsCheckInAppPurchaseKey scrambledKey:0];

When Xcode executes the statement, it runs to the following crashing exception message.

2015-05-22 19:46:02.386 HanDynastyMartialArts[2779:107363] + [MartialArtsCheckInAppPurchaseKey scrambledKey:]: unrecognized selector sent to class 0x10e824878 2015-05-22 19:46:02.390 HanDynastyMartialArts[2779:107363] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[MartialArtsCheckInAppPurchaseKey scrambledKey:]: unrecognized selector sent to class 0x10e824878' * First throw call stack:

In my MartialArtsCheckInAppPurchaseKey.h, I have the following declaration:

// extern int  scrambledKey(int n);


@interface MartialArtsCheckInAppPurchaseKey : NSObject {

}

+(int)  scrambledKey:(int) n;
+(BOOL) scrambledMatch: (int) key;


+(int)  getKeyElement:(int) n;
+(int)  getUIDKeyIndex:(int) n;
+(BOOL) checkInAppPurchaseKey: (int)key;
+(void) setInAppPurchaseKey: (int) key : (BOOL) value;

@end

The selector that Xcode complained is declared in MartialArtsCheckInAppPurchaseKey.m as follows.

- (int)  scrambledKey:(int) n {
  ... 
}
/* scrambledKey */

Can someone help me out of this and tell me what was wrong ? Also why sometimes we declare methods in the header file with '+' and '-' ? What is the difference ?

Thanks

Farn

Upvotes: 1

Views: 533

Answers (2)

Cristik
Cristik

Reputation: 32779

The declaration for scrambledKey:

+(int)  scrambledKey:(int) n;

tells the compiler that the method is a class one (+ stands for class methods, - stands for instance methods). Thus, at runtime you get the unrecognised selector exception as there is no class method named scrambledKey (declaring a method doesn't imply the method exists).

However, you're implementing as an instance method. So, depending on your needs, you either have to change the declaration of the method, or it's implementation. Considering the caller code makes use of a class method, changing the method implementation code (simply replacing the - by a +) would require less modifications. This assuming the implementation doesn't depend on instance members of MartialArtsCheckInAppPurchaseKey, case when you're forced to change the definition (replace + by -, use instances of MartialArtsCheckInAppPurchaseKey to call the method).

Upvotes: 1

l0gg3r
l0gg3r

Reputation: 8954

-scrambledKey: is an instance method (not static method). You need to create an instance of MartialArtsCheckInAppPurchaseKey then call the method on it's instance

MartialArtsCheckInAppPurchaseKey *obj = [MartialArtsCheckInAppPurchaseKey new]
int  kk = [obj scrambledKey:0];

Also please note, that the interface declaration (in MartialArtsCheckInAppPurchaseKey.h file) is wrong, you need to correct it like this

// extern int  scrambledKey(int n);


@interface MartialArtsCheckInAppPurchaseKey : NSObject {

}

// NOTE: I've changed + -> -
-(int)  scrambledKey:(int) n;
+(BOOL) scrambledMatch: (int) key;


+(int)  getKeyElement:(int) n;
+(int)  getUIDKeyIndex:(int) n;
+(BOOL) checkInAppPurchaseKey: (int)key;
+(void) setInAppPurchaseKey: (int) key : (BOOL) value;

@end

Upvotes: 1

Related Questions