//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
在最后的 @end
行,Xcode 告诉我实现不完整。该代码仍然按预期工作,但我是新手,担心我错过了一些东西。据我所知,它是完整的。有时我觉得 Xcode 卡在过去的错误上,但也许我只是失去了理智!
谢谢! -安德鲁
最佳答案
在 9.1.h
中,您漏掉了一个“a”。
-(void) setReal: (double) andImaginary: (double) b;
// ^ here
代码仍然有效,因为在 Objective-C 中,选择器的部分可以没有名称,例如
-(id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y
// ^ ^ ^
这些方法被称为
return [self initWithControlPoints:0.0f :0.0f :1.0f :1.0f];
// ^ ^ ^
选择器名称自然是@selector(initWithControlPoints:::::)
。
因此,编译器会将你的声明解释为
-(void)setReal:(double)andImaginary
:(double)b;
因为你没有提供这个-setReal::
方法的实现,gcc会警告你
warning: incomplete implementation of class ‘Complex’
warning: method definition for ‘-setReal::’ not found
顺便说一句,如果您只想要一个复杂的值但不需要它是一个 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));
关于objective-c - 不完整的实现(xcode 错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6049033/