我目前正在通過「大書呆子牧場」的書中學習Objective-C。我已經重複了第18章的代碼,但是我收到了錯誤。使用64位macos 10.6女巫實際版本的xcode。大書呆子牧場目標-c:第18章 - 繼承
編譯器說:
Build target BMITime
CompileC /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.o BMITime/main.m normal x86_64 objective-c com.apple.compilers.llvm.clang.1_0.compiler cd /Users/pm/Dropbox/objective-c/BMITime setenv LANG en_US.US-ASCII /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch x86_64 -fmessage-length=0 -std=gnu99 -fobjc-arc -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wreturn-type -Wno-implicit-atomic-properties -Wformat -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wuninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-sign-compare -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wno-undeclared-selector -Wno-deprecated-implementations -DDEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -fasm-blocks -Wprotocol -Wdeprecated-declarations -mmacosx-version-min=10.7 -g -Wno-conversion -Wno-sign-conversion "-DIBOutlet=attribute((iboutlet))" "-DIBOutletCollection(ClassName)=attribute((iboutletcollection(ClassName)))" "-DIBAction=void)attribute((ibaction)" -iquote /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-generated-files.hmap -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-own-target-headers.hmap -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-all-target-headers.hmap -iquote /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-project-headers.hmap -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Products/Debug/include -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/DerivedSources/x86_64 -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/DerivedSources -F/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Products/Debug -include /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/PrecompiledHeaders/BMITime-Prefix-bbjacqhmgaerfrhblktcvkpzzntb/BMITime-Prefix.pch -MMD -MT dependencies -MF /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.d --serialize-diagnostics /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.dia -c /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m -o /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.o
/Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:21:10: error: no visible @interface for 'Employee' declares the selector 'setWeightInKilos:' [person setWeightInKilos:96]; ^/Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:22:10: error: no visible @interface for 'Employee' declares the selector 'setHeightInMeters:' [person setHeightInMeters:1.8]; ^/Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:26:22: error: no visible @interface for 'Employee' declares the selector 'bodyMassIndex' float bmi = [person bodyMassIndex]; ^3 errors generated.
我的代碼看起來是這樣的:
的main.m
#import <Foundation/Foundation.h>
#import "Employee.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Erstellt eine Instanz von Person
Employee *person = [[Employee alloc] init];
// Gibt den Instanzvariablen interessante Werte
[person setWeightInKilos:96];
[person setHeightInMeters:1.8];
[person setEmployeeID:15];
// Ruft die Methode bodyMassIndex auf
float bmi = [person bodyMassIndex];
NSLog(@"Employee %d has a BMI of %f", [person employeeID], bmi);
}
return 0;
}
Person.h
#import <Foundation/Foundation.h>
// Die Klasse Person erbt alle Instanzvariablen
// und Methoden, die von der Klasse NSObject definiert wurden
@interface Person : NSObject
{
// Sie hat zwei Instanzvariablen
float heightInMeters;
int weightInKilos;
}
@property float heightInMeters;
@property int weightInKilos;
// Diese Methode berechnet den Body-Mass-Index
- (float)bodyMassIndex;
@end
Employee.h
#import <Foundation/Foundation.h>
#import "Person.h"
@interface Employee : NSObject
{
int employeeID;
}
@property int employeeID;
@end
Employee.m
#import "Employee.h"
@implementation Employee
@synthesize employeeID;
@end
Person.m
#import "Person.h"
@implementation Person
@synthesize heightInMeters, weightInKilos;
- (float)bodyMassIndex
{
//return weightInKilos/(heightInMeters * heightInMeters);
float h = [self heightInMeters];
return [self weightInKilos]/(h * h);
}
@end
就是這樣。目前,Person和Employee是完全獨立的實體(都從NSObject繼承,但不是彼此)。因此,員工類對體重/身高/體重不瞭解。解決辦法是將'@interface Employee:NSObject'更改爲'@interface Employee:Person' – WDUK