xcode 未找到选择器
xcode selector not found
我写了以下声明:
int kk = [MartialArtsCheckInAppPurchaseKey scrambledKey:0];
当Xcode执行该语句时,它运行到以下崩溃异常消息。
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:
在我的 MartialArtsCheckInAppPurchaseKey.h 中,我有以下声明:
// 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
Xcode 抱怨的选择器在MartialArtsCheckInAppPurchaseKey.m 中声明如下。
- (int) scrambledKey:(int) n {
...
}
/* scrambledKey */
有人可以帮我解决这个问题并告诉我哪里出了问题吗?
还有为什么有时我们在头文件中用“+”和“-”声明方法?
有什么不同 ?
谢谢
法恩
-scrambledKey:
是一个实例方法(不是静态方法)。
您需要创建 MartialArtsCheckInAppPurchaseKey
的实例,然后在其实例
上调用该方法
MartialArtsCheckInAppPurchaseKey *obj = [MartialArtsCheckInAppPurchaseKey new]
int kk = [obj scrambledKey:0];
另请注意,接口声明(在MartialArtsCheckInAppPurchaseKey.h文件中)是错误的,您需要这样更正它
// 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
scrambledKey
的声明:
+(int) scrambledKey:(int) n;
告诉编译器该方法是一个 class 方法(+
代表 class 方法,-
代表实例方法)。因此,在运行时你会得到 unrecognised selector
异常,因为没有名为 scrambledKey
的 class 方法(声明一个方法并不意味着该方法存在)。
但是,您是作为实例方法来实现的。因此,根据您的需要,您要么必须更改方法的声明,要么更改它的实现。考虑到调用者代码使用 class 方法,更改方法实现代码(只需将 -
替换为 +
)将需要较少的修改。这假设实现不依赖于 MartialArtsCheckInAppPurchaseKey
的实例成员,当您被迫更改定义时(将 +
替换为 -
,使用 MartialArtsCheckInAppPurchaseKey
的实例调用方法)。
我写了以下声明:
int kk = [MartialArtsCheckInAppPurchaseKey scrambledKey:0];
当Xcode执行该语句时,它运行到以下崩溃异常消息。
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:
在我的 MartialArtsCheckInAppPurchaseKey.h 中,我有以下声明:
// 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
Xcode 抱怨的选择器在MartialArtsCheckInAppPurchaseKey.m 中声明如下。
- (int) scrambledKey:(int) n {
...
}
/* scrambledKey */
有人可以帮我解决这个问题并告诉我哪里出了问题吗? 还有为什么有时我们在头文件中用“+”和“-”声明方法? 有什么不同 ?
谢谢
法恩
-scrambledKey:
是一个实例方法(不是静态方法)。
您需要创建 MartialArtsCheckInAppPurchaseKey
的实例,然后在其实例
MartialArtsCheckInAppPurchaseKey *obj = [MartialArtsCheckInAppPurchaseKey new]
int kk = [obj scrambledKey:0];
另请注意,接口声明(在MartialArtsCheckInAppPurchaseKey.h文件中)是错误的,您需要这样更正它
// 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
scrambledKey
的声明:
+(int) scrambledKey:(int) n;
告诉编译器该方法是一个 class 方法(+
代表 class 方法,-
代表实例方法)。因此,在运行时你会得到 unrecognised selector
异常,因为没有名为 scrambledKey
的 class 方法(声明一个方法并不意味着该方法存在)。
但是,您是作为实例方法来实现的。因此,根据您的需要,您要么必须更改方法的声明,要么更改它的实现。考虑到调用者代码使用 class 方法,更改方法实现代码(只需将 -
替换为 +
)将需要较少的修改。这假设实现不依赖于 MartialArtsCheckInAppPurchaseKey
的实例成员,当您被迫更改定义时(将 +
替换为 -
,使用 MartialArtsCheckInAppPurchaseKey
的实例调用方法)。