将2个类别放入文件中

时间:2013-12-02 05:46:32

标签: ios objective-c

@interface fakeDictionary : NSDictionary
+(NSDictionary *) dictTrivial:(NSString *) ID;
-(NSString *) strIDIfIamTrivial;
@end

@interface fakeDictionaryString : NSString
-(NSDictionary *) dictTrivial;
@end

后跟.m文件

#import "fakeDictionary.h"

@implementation fakeDictionary

+(NSDictionary *) dictTrivial:(NSString *) ID
{
    NSDictionary * dict = @{@"IDorDefaultSelectorValue" : ID};
    return dict;
}
-(NSString *) strIDIfIamTrivial
{
    return self[@"IDorDefaultSelectorValue"];
}


@end

@implementation fakeDictionaryString

-(NSDictionary *) dictTrivial
{
    NSDictionary * dict = [NSDictionary dictTrivial:self]; //I got message that NSDictionary does not support method dictTrivial
}


@end

为什么我收到消息说NSDictionary不支持方法dictTrivial? +(NSDictionary *)dictTrivial:(NSString *)ID在fakeDictionary.h中明确声明

2 个答案:

答案 0 :(得分:1)

我不认为#39;是宣布类别的正确方法。

尝试: 在.h文件中

@interface NSDictionary(fakeDictionary)
+(NSDictionary *) dictTrivial:(NSString *) ID;
-(NSString *) strIDIfIamTrivial;
@end

@interface NSString(fakeDictionaryString)
-(NSDictionary *) dictTrivial;
@end

在.m文件中:

@implementation NSDictionary(fakeDictionary)

+(NSDictionary *) dictTrivial:(NSString *) ID
{
    NSDictionary * dict = @{@"IDorDefaultSelectorValue" : ID};
    return dict;
}
-(NSString *) strIDIfIamTrivial
{
    return self[@"IDorDefaultSelectorValue"];
}


@end

@implementation NSString(fakeDictionaryString)

-(NSDictionary *) dictTrivial
{
    NSDictionary * dict = [NSDictionary dictTrivial:self]; //I got message that NSDictionary does not support method dictTrivial
}


@end

答案 1 :(得分:1)

您的fakeDictionary继承自NSDictionary,而不是Category

所以你应该调用这样的方法:

NSDictionary * dict = [fakeDictionary dictTrivial:self];