使用未声明的标识符

时间:2014-11-23 20:02:31

标签: objective-c compiler-errors xcode6 nsobject

我正在从The Big Nerd Ranch Guide第2版的书中学习Objective-C编程。我已轻松地完成了第18章,但现在Xcode已更新,我遇到语法错误"使用未声明的标识符' heightInMeters'。这是我的代码,它是Objective-c,带有NSObject的子类。

***AppDelegate.h***
#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

{
    // BNRPerson has two instance variables
    float _heightInMeters;
    int _weightInKilos;
}
// BNRPerson has methods to read and set its instance variables
- (float)heightInMeters;
- (void)setHeightInMeters:(float)h;
- (int)weightInKilos;
- (void)setWeightInKilos:(int)w;

// BNRPerson has a method that calculates the Body Mass Index
- (float)bodyMassIndex;

@end


***AppDelegate.m***
#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    - (float)heightInMeters *USE OF UNDECLARED IDENTIFIER 'heightInMeters'
    {
        return _heightInMeters;
    }
    - (void)setHeightInMeters:(float)h
    {
        _heightInMeters=h;
    }
    - (int)weightInKilos
    {
        return _weightInKilos;
    }
    - (void)setWeightInKilos:(int)w
    {
        _weightInKilos=w;
    }
    - (float)bodyMassIndex
    {
        return _weightInKilos / (_heightInMeters * _heightInMeters);
    }
 }

- (void)applicationWillTerminate:(NSNotification *)aNotification {
     // Insert code here to tear down your application
}

@end

2 个答案:

答案 0 :(得分:1)

看起来你正在将方法放在方法中。我不知道为什么。尝试:

//
//  AppDelegate.m
//  Test
//
//  Created by JK on 11/23/14.
//  
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (float)heightInMeters
{
    return _heightInMeters;
}

- (void)setHeightInMeters:(float)h
{
    _heightInMeters=h;
}

- (int)weightInKilos
{
    return _weightInKilos;
}

- (void)setWeightInKilos:(int)w
{
    _weightInKilos=w;
}

- (float)bodyMassIndex
{
    return _weightInKilos / (_heightInMeters * _heightInMeters);
}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

@end

答案 1 :(得分:0)

您需要在applicationDidFinishLaunching中使用您的方法实现:

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

 }

- (float)heightInMeters
    {
        return _heightInMeters;
    }
- (void)setHeightInMeters:(float)h
    {
        _heightInMeters=h;
    }
- (int)weightInKilos
    {
        return _weightInKilos;
    }
- (void)setWeightInKilos:(int)w
    {
        _weightInKilos=w;
    }
- (float)bodyMassIndex
    {
        return _weightInKilos / (_heightInMeters * _heightInMeters);
    }

- (void)applicationWillTerminate:(NSNotification *)aNotification {
     // Insert code here to tear down your application
}