1

私は2つのクラスを持っていWorkoutますAction. には、保存されたデータとデータを取得し、それらの を作成するWorkoutというメソッドがあります。現在、このクラスには、このメソッドを簡単にするために記述したファクトリ メソッドがありますが、ファクトリ メソッドの 1 つを呼び出そうとすると、Xcode から「セレクタ 'initWithName:andCount` の既知のクラス メソッドがありません」と表示されます。associateActionsAndCountsNSStringNSIntegerActionAction

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(アクションとカウントは ivar です)

-(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」の既知のクラス メソッドはありません

4

2 に答える 2

6

ここでいくつかの問題があります。

第 1 に、コード内で " " にnum渡す " " パラメータはオブジェクトであり、宣言したメソッドが想定しているオブジェクトではありません。andCountNSStringNSInteger

第 2 に、この種の「ファクトリ」メソッドを実行してinitWithName: andCount:いる場合は、allocinit" " のように別の名前で宣言して+(Action *) actionWithName: andCount:ください。そうしないと、後でメモリの問題が発生した場合に、このコードを見て、あなた (またはさらに悪いことに、あなたではない別のプログラマー) が非常に混乱することになります。

于 2013-07-09T15:09:40.373 に答える