不完整的实现(xcode 错误?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6049033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:03:45  来源:igfitidea点击:

Incomplete implementation (xcode mistake?)

objective-cxcode

提问by jag

// 9.1.h

// 9.1.h

#import <Foundation/Foundation.h>


@interface Complex : NSObject 
{

    double real;
    double imaginary;

}

@property double real, imaginary;
-(void) print;
-(void) setReal: (double) andImaginary: (double) b;
-(Complex *) add: (Complex *) f;

@end


#import "9.1.h"


@implementation Complex

@synthesize real, imaginary;

-(void) print
{
    NSLog(@ "%g + %gi ", real, imaginary);
}

-(void) setReal: (double) a andImaginary: (double) b
{
    real = a;
    imaginary = b;
}

-(Complex *) add: (Complex *) f
{
    Complex *result = [[Complex alloc] init];

    [result setReal: real + [f real] andImaginary: imaginary + [f imaginary]];

    return result;

}
@end

On the final @endline, Xcode is telling me the implementation is incomplete. The code still works as expected, but I'm new at this and am worried I've missed something. It is complete as far as I can tell. Sometimes I feel like Xcode hangs on to past errors, but maybe I'm just losing my mind!

在最后@end一行,Xcode 告诉我实现不完整。代码仍然按预期工作,但我是新手,担心我错过了一些东西。据我所知,它是完整的。有时我觉得 Xcode 会被过去的错误所困扰,但也许我只是失去了理智!

Thanks! -Andrew

谢谢!-安德鲁

回答by kennytm

In 9.1.h, you have missed an 'a'.

在 中9.1.h,您错过了一个“a”。

-(void) setReal: (double) andImaginary: (double) b;
//                       ^ here


The code is still valid, because in Objective-C a selector's part can have no name, e.g.

代码仍然有效,因为在 Objective-C 中,选择器的部分可以没有名称,例如

-(id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y
//                                    ^           ^           ^

these methods are called as

这些方法被称为

return [self initWithControlPoints:0.0f :0.0f :1.0f :1.0f];
//                                      ^     ^     ^

and the selector name is naturally @selector(initWithControlPoints::::).

选择器名称自然是@selector(initWithControlPoints::::).

Therefore, the compiler will interpret your declaration as

因此,编译器会将您的声明解释为

-(void)setReal:(double)andImaginary
              :(double)b;

since you have not provided the implementation of this -setReal::method, gcc will warn you about

由于您尚未提供此-setReal::方法的实现,因此gcc 会警告您

warning: incomplete implementation of class ‘Complex'
warning: method definition for ‘-setReal::' not found


BTW, if you just want a complex value but doesn't need it to be an Objective-C class, there is C99 complex, e.g.

顺便说一句,如果你只想要一个复数值但不需要它是一个 Objective-C 类,那么有C99 complex,例如

#include <complex.h>

...

double complex z = 5 + 6I;
double complex w = -4 + 2I;
z = z + w;
printf("%g + %gi\n", creal(z), cimag(z));