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

标签 objective-c xcode

//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/

相关文章:

objective-c - 设置 PopUpButton 的操作

ios - 快速编译错误

xcode - Xamarin/XCode 注册配置文件错误

objective-c - 如何在 iOS 中实现防止重复投票的基本投票机制?

ios - 我无法通过标签 objective-c 更改标签文本

json - Swift3 Json解析-如何访问相关字段

ios - 如何在调试我的应用程序时跳过内存读取?

xcode - Xcode Instrument 的反汇编时间分析的可靠性

SwiftUI 不同子项对齐和 GeometryReader

ios - 将 UIImage 数据保存到 iOS 中的照片库后取回