首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未公开的类方法

未公开的类方法
EN

Stack Overflow用户
提问于 2013-07-09 15:07:56
回答 2查看 220关注 0票数 1

我有两门课,WorkoutActionWorkout有一个名为associateActionsAndCounts的方法,它接收存储的NSString数据和NSInteger数据,并为它们创建一个Action。现在,Action类有我编写的工厂方法来简化这个方法,但是当我尝试调用其中一个工厂方法时,Xcode告诉我“没有已知的用于选择器‘initWithName:andCount`’的类方法。

Action.h

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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)

代码语言:javascript
运行
复制
-(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的建议,我的代码现在如下所示:

代码语言:javascript
运行
复制
-(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

代码语言:javascript
运行
复制
+(Action *)actionWithName:(NSString*)name;
+(Action *)actionWithName:(NSString*)name andCount:(NSInteger*)count;
+(Action *)actionWithName:(NSString*)name andCount:(NSInteger*)count andImage:(UIImage*)image;

但我还是会犯同样的错误。

对于选择器"actionWithName:andCount“,没有已知的类方法

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-09 15:09:40

你这里有几个问题。

首先,在代码中传递给"andCount“的”andCount“参数是一个NSString对象,而不是您声明的方法所期望的NSInteger对象。

第二,如果您正在执行这种“工厂”方法,请不要将其命名为"initWithName: andCount:“,因为这意味着您希望在"init”之前使用"alloc“方法。以不同的名称声明它,如"+(Action *) actionWithName: andCount:“。否则,您(或者更糟的是,另一个不是您的程序员)在查看这段代码时会非常困惑,如果接下来会出现内存问题。

票数 6
EN

Stack Overflow用户

发布于 2013-07-09 15:51:10

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17551765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档