Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 多方法警告_Objective C - Fatal编程技术网

Objective c 多方法警告

Objective c 多方法警告,objective-c,Objective C,我目前正在学习目标C,在此过程中,我制作了下面这个愚蠢的小程序。该程序编译得很好-但是我得到警告“多个名为'-setName:'found'的方法” 我只对该方法进行了一次接口和实现 此警告是什么意思?我如何更正它 #import <Foundation/Foundation.h> // these are the three yoga-exercises we can perform typedef enum { kCobra, kUniversal,

我目前正在学习目标C,在此过程中,我制作了下面这个愚蠢的小程序。该程序编译得很好-但是我得到警告“多个名为'-setName:'found'的方法”

我只对该方法进行了一次接口和实现

此警告是什么意思?我如何更正它

#import <Foundation/Foundation.h>

// these are the three yoga-exercises we can perform

typedef enum {
    kCobra,
    kUniversal,
    kDog
} ExerciseName;

// translating our variables into human

NSString *nameExercise (ExerciseName nameExercise)
{
    switch (nameExercise) {
        case kCobra:
            return @"Cobra Pose";
            break;
        case kUniversal:
            return @"Universal Stretch";
            break;
        case kDog:
            return @"Dog Pose";
            break;
    }

    return @"no clue!";

} // nameExercise


@interface Exercise : NSObject
{
    ExerciseName name;
}

-(void) setName: (ExerciseName) name;
-(void) exerciseDo;

@end


@implementation Exercise

-(void) setName: (ExerciseName) n {
    name = n;
} // setName

-(void) exerciseDo {
    NSLog(@"Exercise: %@",
          nameExercise(name));
}

@end


void executeExercises(id exercises[], int count) {

    int i;

    for(i=0; i<count; i++) {
        id exercise = exercises[i];
        [exercise exerciseDo];
    }
}

int main (int argc, const char * argv[]) {

    id exercises[1];

    exercises[0] = [Exercise new]; // initiating an object of class Exercise
    [exercises[0]   setName:kDog];

    executeExercises(exercises, 1);

    return 0;

} //main
#导入
//这是我们可以做的三个瑜伽练习
类型定义枚举{
科科布拉,
库尼韦尔,
kDog
}练习名称;
//将我们的变量转换为人类变量
NSString*名称练习(ExerciseName名称练习)
{
开关(名称练习){
案例kCobra:
返回@“眼镜蛇姿势”;
打破
案例kUniversal:
返回@“通用拉伸”;
打破
案例kDog:
返回"狗式";;
打破
}
返回@“没有线索!”;
}//名称练习
@接口练习:NSObject
{
练习名称;
}
-(void)集合名:(ExerciseName)名称;
-(无效)行使;
@结束
@实施工作
-(void)集合名:(ExerciseName)n{
name=n;
}//集合名
-(无效)行使{
NSLog(@“练习:%@),
姓名(姓名);;
}
@结束
void executeExercises(id练习[],整数计数){
int i;

对于(i=0;i消息的含义是,翻译中有多个选择器的名称为
setName:
(即,它至少在所有包含的标题中的其他位置声明)。编译器可能会选择错误的选择器(这可能会引入未定义的行为)

通常,您可以使用以下一种(或多种)方法纠正问题:

1) 将该方法重命名为唯一名称:例如,如果未在其他翻译中使用,
setExerciseName
可能可以

2) 匹配其他选择器的签名。例如,
setName:(NSString*)name

3) 使用类型安全:

Exercise * ex = [Exercise new];
[ex setName:kCobra];
4) 将变量强制转换为以下类型:
[(Exercise*)Exercise setName:kCobra];

5) 使用新变量还原类型:
Exercise*ex=Exercise;

由于您已将var声明为
id
,因此您已删除了该类型,这意味着该对象可能会响应任何可见的选择器。通常,您不应以这种方式删除该类型,除非确实必要

我看到的最佳方法是1和3的组合:

[ex setExerciseName:kCobra];

该消息的含义是,翻译中有多个选择器的名称为
setName:
(即,它至少在所有包含的标题中的其他位置声明)。编译器可能会选择错误的选择器(这可能会引入未定义的行为)

通常,您可以使用以下一种(或多种)方法纠正问题:

1) 将该方法重命名为唯一名称:例如,如果未在其他翻译中使用,
setExerciseName
可能可以

2) 匹配其他选择器的签名。例如,
setName:(NSString*)name

3) 使用类型安全:

Exercise * ex = [Exercise new];
[ex setName:kCobra];
4) 将变量强制转换为以下类型:
[(Exercise*)Exercise setName:kCobra];

5) 使用新变量还原类型:
Exercise*ex=Exercise;

由于您已将var声明为
id
,因此您已删除了该类型,这意味着该对象可能会响应任何可见的选择器。通常,您不应以这种方式删除该类型,除非确实必要

我看到的最佳方法是1和3的组合:

[ex setExerciseName:kCobra];

+1.这样想……如果六个月后,你从未见过的人必须阅读你的代码,哪一行代码最有意义,让读者最能理解你的意图?+1.这样想……如果六个月后,你从未见过的人必须阅读你的代码,哪一行代码最有意义让读者充分理解你的意图?