Objective-C中的类别不起作用 [英] Categories in Objective-C aren't working
问题描述
我正在开发一个需要部署到iOS 3.1.3的iOS应用程序。我需要扩展NSData类的一些功能,并在NSData + Base64中使用以下代码(截断以显示有趣的部分):
I'm developing an iOS application that needs to deploy to iOS 3.1.3. I need to extend some of the functionality of the NSData class and am using the following code inside NSData+Base64 (truncated to show the interesting part):
[...]
@interface NSData (Base64)
+ (NSData *)dataFromBase64String:(NSString *)aString;
- (NSString *)base64EncodedString;
@end
NSData + Base64.m
NSData+Base64.m
@implementation NSData (Base64)
[...]
//
// base64EncodedString
//
// Creates an NSString object that contains the base 64 encoding of the
// receiver's data. Lines are broken at 64 characters long.
//
// returns an autoreleased NSString being the base 64 representation of the
// receiver.
//
- (NSString *)base64EncodedString
{
size_t outputLength;
char *outputBuffer =
NewBase64Encode([self bytes], [self length], true, &outputLength);
NSString *result =
[[[NSString alloc]
initWithBytes:outputBuffer
length:outputLength
encoding:NSASCIIStringEncoding]
autorelease];
free(outputBuffer);
return result;
}
@end
但是,当我尝试向此选择器发送消息时:
However, when I try to message this selector:
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
NSString *hash = [HMAC base64EncodedString];
我收到以下错误:
-[NSConcreteData base64EncodedString]: unrecognized selector sent to instance 0x6146e70
2010-11-09 13:44:41.443 SpringboardApplication[21318:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData base64EncodedString]: unrecognized selector sent to instance 0x6146e70'
I阅读了很多有关类别问题的iOS 3.1.x.我尝试添加标志 -all_load
和 -ObjC
(单独和一起)无济于事。我真的很感激如何使这个选择器工作的方向。
I read a lot about iOS 3.1.x having problems with categories. I tried adding the flags -all_load
and -ObjC
(both separately and together) to no avail. I would really appreciate some direction of how to get this selector to work.
谢谢!
推荐答案
您的类别似乎没有被编译或链接到您正在使用它的同一目标。您应该确保NSData + Base64.m被标记为由使用它的同一目标编译,方法是获取两个文件的信息并比较它们分配给它们的目标。
It really seems like your category isn't being compiled or linked into the same target that you're using it from. You should make sure that NSData+Base64.m is marked to be compiled by the same target that it's being used from by getting info on the two files and comparing the targets they're assigned to.
您可以执行的测试是向NSData + Base64.m添加带有#error错误消息的行,这将导致构建在到达时失败文件。像这样:
A test you can perform is to add a line with an #error error message to NSData+Base64.m, which will cause the build to fail when it gets to that file. Like this:
#error We're now compiling NSData+Base64.m
然后查看哪个目标无法编译。
Then look and see which target fails to compile.
这篇关于Objective-C中的类别不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!