类方法未公开

时间:2013-07-09 15:07:57

标签: objective-c

我有两个课程WorkoutActionWorkout有一个名为associateActionsAndCounts的方法,该方法获取存储的NSString数据和NSInteger数据,并为其创建Action。现在,Action类有我编写的工厂方法,使这个方法更简单,但是当我尝试调用其中一个工厂方法时,Xcode告诉我“没有已知的选择器类方法'initWithName:andCount` ”

Action.h

#import <Foundation/Foundation.h>
@interface Action : NSObject
+(Action *)initWithName:(NSString*)name;
+(Action *)initWithName:(NSString*)name andCount:(NSInteger*)count;
+(Action *)initWithName:(NSString*)name andCount:(NSInteger*)count andImage:(UIImage*)image;
@property UIImage *image;
@property NSString *name;
@property NSInteger *count;
@end

Action.m

#import "Action.h"
@implementation Action
@synthesize image;
@synthesize name;
@synthesize count;
#pragma mark - factory methods
+(Action *)initWithName:(NSString *)name {
    Action *newAct = [Action alloc];
    [newAct setName:name];
    return newAct;
}
+(Action *)initWithName:(NSString*)name andCount:(NSInteger*)count {
    Action *newAct = [Action alloc];
    [newAct setName:name];
    [newAct setCount:count];
    return  newAct;
}
+(Action *)initWithName:(NSString *)name andCount:(NSInteger *)count andImage:(UIImage *)image {
    Action *newAct = [Action alloc];
    [newAct setName:name];
    [newAct setCount:count];
    [newAct setImage:image];
    return newAct;
}
@end

Workout.m - associateActionsAndCounts(行动和计数是ivars)

-(void)associateActionsAndCounts {
    for (int i=0;i<actions.count;i++) {
        NSString *name = [actions objectAtIndex:i];
        NSString *num = [counts objectAtIndex:i];
        Action *newAction  = [Action initWithName:name andCount:num]; //no known class method for selector
        [actionsData addObject:newAction];
    }
}

编辑:

根据Michael Dautermann的建议,我的代码现在看起来像这样:

-(void)associateActionsAndCounts {
    for (int i=0;i<actions.count;i++) {
        NSString *actionName = [actions objectAtIndex:i];
        NSInteger num = [[NSString stringWithString:[counts objectAtIndex:i]] intValue];
        Action *newAction  = [Action actionWithName:actionName andCount:num];
        [actionsData addObject:newAction];
    }
}

Action.h

+(Action *)actionWithName:(NSString*)name;
+(Action *)actionWithName:(NSString*)name andCount:(NSInteger*)count;
+(Action *)actionWithName:(NSString*)name andCount:(NSInteger*)count andImage:(UIImage*)image;

但我仍然遇到同样的错误。

  

选择器“actionWithName:andCount”

没有已知的Class方法

2 个答案:

答案 0 :(得分:6)

这里有一些问题。

第一,您在代码中传入“num”的“andCount”参数是NSString对象,而不是该方法的NSInteger对象你已宣布期待。

第二,如果您正在使用这种“工厂”方法,请不要将其命名为“initWithName: andCount:”,因为这意味着您需要在“alloc”之前使用“init”方法。 +(Action *) actionWithName: andCount:”。使用其他名称声明它,例如“{{1}}”。否则,你(或者更糟的是,另一个不是你的程序员)在查看这段代码时会非常困惑,如果在路上有内存问题的话。

答案 1 :(得分:0)

不知何故Workout.hWorkout.m不在项目文件夹中,而是在另一个项目文件夹中。因此Action.h未包括在内。