2014-12-21 25 views
-2

.m文件目標C的Xcode頭部差錯

#import "BNREmployee.h" 

@interface BNREmployee() 

@property (weak) IBOutlet NSWindow *window; 

@end 
@implementation BNREmployee 
- (double)yearsOfEmployment { 
    **Use of undeclared identifier 'applicationDidFinishLaunching'** -(void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    // Do I have a non-nil hireDate? 
    if (self.hireDate) { 
     // NSTimeInterval is the same as double 
     NSDate *now = [NSDate date]; 
     NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate]; 
     return secs/31557600.0; // Seconds per year 
    } else { 
     return 0; 
    } 
} 
- (float) bodyMassIndex 
{ 
    float normalBMI = [super bodyMassIndex]; 
    return normalBMI * 0.9; 
} 
} 
- (void)applicationWillTerminate:(NSNotification *)aNotification { 
    // Insert code here to tear down your application 
} 


@end 

h文件

#import "BNRPerson.h" 

@interface BNREmployee : BNRPerson 

@property (nonatomic) unsigned int employeeID; 
@property (nonatomic) unsigned int officeAlarmCode; 
@property (nonatomic) NSDate *hireDate; 
- (double)yearsOfEmployment; 

@end 

我被提示在另一個答案後使用{ - (雙)yearsOfEmployment但線路是另一個後其中變量'applicationDidFinishLaunching'沒有在其他地方聲明,因爲它是已經在Xcode中實現的通用變量。這就是爲什麼我想知道如何讓我的語法正確,以避免編譯器錯誤。

+2

看起來你很長時間沒編程。在開始編程之前,您應該瞭解一種語言的語法。像這裏一樣在網上獲得一些教程https://www.thenewboston.com/videos.php?cat=33 –

+0

如果您可以解決您的問題以關閉此帖,請標記正確的答案。 –

回答

0

你不能在另一個方法中聲明一個方法。你應該有你的方法是這樣的:

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

} 

- (double)yearsOfEmployment { 

    // Do I have a non-nil hireDate? 
    if (self.hireDate) { 
     // NSTimeInterval is the same as double 
     NSDate *now = [NSDate date]; 
     NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate]; 
     return secs/31557600.0; // Seconds per year 
    } else { 
     return 0; 
    } 
} 

- (float) bodyMassIndex { 
    float normalBMI = [super bodyMassIndex]; 
    return normalBMI * 0.9; 
} 

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

而且裏面的另一種方法,你可以打電話給你的預定義的方法是這樣的:

[self yearsOfEmployment]; 

CGFloat myFloat = [self bodyMassIndex]; 

但通常你不應該有任何申請方法內另一類比AppDelegateAppDelegate處理基本的東西,如後臺進程和將在應用程序開始時調用的東西。

0

applicationDidFinishLaunching:不是「全局變量」。這是符合UIApplicationDelegate的類必須實現的方法的名稱。所以把它從你實施的yearsOfEmployment中拿出來。

例如,

- (double)yearsOfEmployment { 
    // Do I have a non-nil hireDate? 
    if (self.hireDate) { 
     // NSTimeInterval is the same as double 
     NSDate *now = [NSDate date]; 
     NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate]; 
     return secs/31557600.0; // Seconds per year 
    } else { 
     return 0; 
    } 
} 

但是,你必須超越這些其他問題。你的BNREmployee類肯定看起來像一個模型類。我不能爲了我的生活而揣測爲什麼你會在裏面有applicationDidFinishLaunching:applicationWillTerminate:這樣的東西。這些方法只屬於一個地方:單個類和有效的單例 - 在您的應用程序中實現UIApplicationDelegate