2

我刚刚开始进行 iOS 开发,由于警告而有点卡住了。构建成功,但这个警告让我很困扰。我检查了其他一些答案,但无法弄清楚出了什么问题。

Waring - 不完整的实现

复数.h

#import <Foundation/Foundation.h>

@interface ComplexNumbers : NSObject

-(void) setReal: (double)a;
-(void) setImaginary: (double)b;
-(void) print; // display as a + bi

-(double) real;
-(double) imaginary;

@end

复数.m

#import "ComplexNumbers.h"

@implementation ComplexNumbers  // Incomplete implementation

{
double real;
double imaginary;
}

-(void) print
{
    NSLog(@"%f + %fi",real,imaginary);
}
-(void) setReal:(double)a
{
    real = a;
}
-(void) setImaginary:(double)b
{
    imaginary = b;
}

@end
4

3 回答 3

3

你的问题是你的界面说有realimaginary方法,但你还没有实现这些。更好的是,通过将它们定义为属性,让编译器为您合成setterrealimaginarygetter 方法,并且您的代码大大简化:

@interface ComplexNumbers : NSObject

@property (nonatomic) double real;
@property (nonatomic) double imaginary;

-(void) print; // display as a + bi

@end

@implementation ComplexNumbers

-(void) print
{
    NSLog(@"%f + %fi", self.real, self.imaginary);
}

@end
于 2013-02-23T03:43:31.017 回答
2

您还没有实现这些属性获取器:

-(double) real;
-(double) imaginary;

您可以实现它们:

-(double) real { return _real; }
-(double) imaginary { return _imaginary; }

或者让编译器通过在标题中将它们声明为属性来为您完成:

@property(nonatomic) double real;
@property(nonatomic) double imaginary;

在 .m 文件中:

@synthesize real = _real, imaginary = _imaginary;

其中 _ 是实例成员。

于 2013-02-23T03:42:25.913 回答
0

试试这个,

#import "ComplexNumbers.h"

@implementation ComplexNumbers  // Incomplete implementation

{
double real;
double imaginary;
}

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

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

{
imaginary = b;
}
 -(double) real
{
   return real;
}
-(double) imaginary
{
   return imaginary;
}

@end
于 2013-02-23T03:50:58.403 回答