我有两个班,Workout
和Action
。Workout
有一个名为 的方法associateActionsAndCounts
,它获取存储的NSString
数据和NSInteger
数据并为它们创建一个Action
。现在,Action
该类具有我为使该方法更简单而编写的工厂方法,但是当我尝试调用其中一种工厂方法时,Xcode 告诉我“选择器'initWithName:andCount` 没有已知的类方法”。
动作.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
动作.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];
}
}
动作.h
+(Action *)actionWithName:(NSString*)name;
+(Action *)actionWithName:(NSString*)name andCount:(NSInteger*)count;
+(Action *)actionWithName:(NSString*)name andCount:(NSInteger*)count andImage:(UIImage*)image;
但我仍然遇到同样的错误。
选择器“actionWithName:andCount”没有已知的类方法