objective-c - 使用未声明的标识符

标签 objective-c compiler-errors xcode6 nsobject

我正在从《大 Nerd 牧场指南》第二版中学习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

最佳答案

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

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

关于objective-c - 使用未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093651/

相关文章:

objective-c - UIGestureRecognizer State Changed 和 touchesMoved 之间有什么区别?

Perl: 错误信息: can´t locate ... in @INC

python - 为什么 Python 编译这段代码没有抛出错误?

ios - 在 Xcode 6 中为 Ad Hoc Distribution 选择配置文件

xcode6 - 如何在 Xcode 6 中删除特定尺寸类预览?

objective-c - 适用于 NSArray 中包含的字符串数量

ios - 从 VoiceOver 隐藏 UITableViewCell

ios - 自调整单元格对我不起作用

c++ - 为什么这个C++程序编译失败?

ios - 当在 MainView 上按下按钮时,如何在 SecondViewController 上显示不同的图像