2014-11-23 121 views
-1

我正在學習The Big Nerd Ranch Guide第二版書中的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 
+0

這是什麼應該是:' - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {(float)heightInMeters *使用UNDECLARED IDENTIFIER'heightInMeters'' – 2014-11-23 20:18:32

回答

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 
+0

感謝您回答我的問題,此代碼在編譯器中工作。你的幫助爲我節省了很多時間,查看我的語法,謝謝你的時間。 – Kevin 2014-11-24 05:19:35

+0

接受我的回答顯示您的感謝將幫助我,您和SO社區:) – Jackson 2014-11-24 07:12:02

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 
}