Reputation: 2321
I've got two classes, Workout
and Action
. Workout
has a method called associateActionsAndCounts
, which takes stored NSString
data and NSInteger
data and creates an Action
for them. Now, the Action
class has factory methods that I've written to make this method simpler, however when I try and call one of the factory methods, Xcode tells me "No known class method for selector '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
(actions and counts are 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];
}
}
Edit:
With Michael Dautermann's suggestions, my code looks like this now:
-(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;
But i'm still getting the same error.
No known Class method for selector "actionWithName:andCount"
Upvotes: 1
Views: 221
Reputation: 2321
Somehow Workout.h
and Workout.m
weren't in the project folder, but rather in another project folder. So Action.h
wasn't included.
Upvotes: 0
Reputation: 89559
You have a few problems here.
Number one, the "num
" parameter you're passing into "andCount
" in your code is a NSString
object and not the NSInteger
object that the method you've declared is expecting.
Number two, if you're doing this kind of "factory" method, don't name it "initWithName: andCount:
" because that implies you're expecting an "alloc
" method before the "init
". Declare it with a different name, like "+(Action *) actionWithName: andCount:
". Otherwise, you (or worse, another programmer who is not you) will be mightily confused when looking at this code if there are memory issues down the road.
Upvotes: 6