目标c中隐式声明函数的问题

时间:2011-01-25 14:09:58

标签: objective-c function declaration

我试图更多地了解Objective-c,目前我被卡住了。我有4个错误,都是一样的。 “隐含的功能声明”,我用谷歌搜索,但我找不到解决方案。

RadioStation .h

#import <Cocoa/Cocoa.h>
@interface RadioStation : NSObject {
  NSString* name;
  double frequency;
  char band;
}
+(double)minAMFrequency;
+(double)maxAMFrequency;
+(double)minFMFrequency;
+(double)maxFMFrequency;
-(id)initWithName:(NSString*)newName atFrequency:(double)newFrequency withBand:(char)newBand;
-(NSString*)name;
-(double)frequency;
-(char)band;
-(void)setName:(NSString*)newName;
-(void)setFrequency:(double)newFrequency;
-(void)setBand:(char)newBand;
@end

RadioStation .m

#import "RadioStation.h"

@implementation RadioStation
+(double)minAMFrequency{
 return 520.0;
};
+(double)maxAMFrequency{
     return 1610.0;
};
+(double)minFMFrequency{
 return 88.3;
};
+(double)maxFMFrequency{
 return 107.9;
};
-(id)initWithName:(NSString*)newName atFrequency:(double)newFrequency withBand:(char)newBand{
 self = [super init];
 if(self != nil){
  name = [newName retain];
  band = newBand;
  if (band == 'F') {
   if (newFrequency > maxFMFrequency()) {
    frequency = maxFMFrequency();
   }else if (newFrequency < minFMFrequency()) {
    frequency = minFMFrequency();
   }else {
    frequency = newFrequency;
   }

  }else if (band == 'A') {
   if (newFrequency > maxAMFrequency()) {
    frequency = maxAMFrequency();
   }else if (newFrequency < minAMFrequency()) {
    frequency = minAMFrequency();
   }else {
    frequency = newFrequency;
   }
  }
 }
 return self;
}
@end

if (newFrequency > maxFMFrequency()) {
if (newFrequency < minFMFrequency()) {
if (newFrequency > maxAMFrequency()) {
if (newFrequency < minAMFrequency()) {

所有人都说“隐含的功能宣告”

Thanx提前, Dietger

4 个答案:

答案 0 :(得分:9)

这些是类方法,因此您需要按如下方式更改每个方法:

if (newFrequency > [RadioStation maxFMFrequency]) {
if (newFrequency < [RadioStation minFMFrequency]) {
if (newFrequency > [RadioStation maxAMFrequency]) {
if (newFrequency < [RadioStation minAMFrequency]) {

答案 1 :(得分:3)

你正在混合方法和功能。

您用

调用该代码的代码
if (newFrequency > maxFMFrequency()) {

期望看到像

这样的函数的声明
double maxFMFrequency()

作为C函数实现 - 这将无法从对象获取数据,因此您需要使用methof

标题确实将方法声明为

+(double)maxFMFrequency;

但需要被称为

if (newFrequency > [RadioStation maxFMFrequency])

答案 2 :(得分:2)

我认为这可能是因为你正在混合使用C和Objective C语法。

尝试:

if (newFrequency > [self maxFMFrequency])

答案 3 :(得分:1)

我遇到了同样的问题。通过修改函数调用进行排序,如下所示

//function declaration 
-(void) downloadPage:(NSString *)url;

//function definition 
-(void) downloadPage:(NSString *)url
{
userOutput.text = url;
}

//and now the fixed call to downloadPage

-(IBAction) onButtonOneClicked:(id) sender
{
userOutput.text = @"please wait...";
    //fixed call to downloadPage
[self downloadPage:[userInput text]];
}