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

jag*_*jag 5 xcode objective-c

// 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
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)

在最后@end一行,Xcode告诉我实现不完整.代码仍然按预期工作,但我是新手,我担心我错过了什么.据我所知,这是完整的.有时我觉得Xcode会挂起过去的错误,但也许我只是在失去理智!

谢谢!-安德鲁

ken*_*ytm 10

9.1.h,你错过了'a'.

-(void) setReal: (double) andImaginary: (double) b;
//                       ^ here
Run Code Online (Sandbox Code Playgroud)

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

-(id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y
//                                    ^           ^           ^
Run Code Online (Sandbox Code Playgroud)

这些方法称为

return [self initWithControlPoints:0.0f :0.0f :1.0f :1.0f];
//                                      ^     ^     ^
Run Code Online (Sandbox Code Playgroud)

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

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

-(void)setReal:(double)andImaginary
              :(double)b;
Run Code Online (Sandbox Code Playgroud)

由于你没有提供这种-setReal::方法的实现,gcc会警告你

warning: incomplete implementation of class ‘Complex’
warning: method definition for ‘-setReal::’ not found
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果你只是想要一个复杂的值,但不需要它是一个Objective-C类,那就有C99复合体,例如

#include <complex.h>

...

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