No Visible @interface


When I tried to compile the following, an error message appeared:

Can someone point out what I did wrong with the code?


#import <Foundation/Foundation.h>

@interface Person : NSObject
    //It has two instance variables
    float heightInMeters;
    int weightInKilos;

// You will be able to set those instance variables using these methods
- (float)heightInMeters;
- (void)setHeightInMeters:(float)h;
- (int)weightInKilos;
- (void)setWeightInKilos:(int)w;

//This method calculates the Body Mass Index
- (float)bodyMassIndex;


#import "Person.h"

@implementation Person

- (float)heightInMeters
    return heightInMeters;

- (void)setHeightInMeters:(float)h
    heightInMeters = h;

- (int)weightInKilos
    return weightInKilos;

- (void)setWeightInKilos:(int)w
    weightInKilos = w;
    return weightInKilos / (heightInMeters * heightInMeters);



#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])

    @autoreleasepool {
        // Create an instance of Person
        Person *person = [[Person alloc] init];
        // Give the instance variables interesting values
        [person setWeightInKilos:9.6];
        [person setHeightInMeters:1.8];
        // Call the bodyMassIndex method
        float bmi = [person bodyMassIndex];
        NSLog(@"person (%d, %f) has a BMI of %f", [person weightInKilos], [person heightInMeters], bmi);
    return 0;



When you had the error, did you have the weightInKilos method in Person.h spelled out correctly and without any case errors?

I have copied and pasted your code; it built without any errors.

Its output:

2012-08-14 22:38:41.163 coolandsmartrr[5273:403] person (9, 1.800000) has a BMI of 2.777778


I copied all of my code again from above, but the same error persists.
Could it be the settings in Xcode or something?


I think the problem is weightInKilos is declared as an int in the interface and implementation but you are sending a float in main.



Mitch, following your advice, I changed a line in the Person.h file to this:



However, when I compiled, the same error message appeared.


One likely cause of this error is that you did not use the same method name as in the declaration. Cases of letters in names does matter.

When invoking a method, make sure that you use the same method name as in the declaration.


I get the same error unless I change the setters to dot notation as in the below? Does it have to be dot notation (which is fine with me - but just trying to understand) -

   //[person setweightInKilos:96];
   //[person setheightInMeters:1.8];


There are capitalization errors:
[color=#FF0000]setweightInKilos[/color] —> setWeightInKilos
[color=#FF0000]setheightInMeters[/color] —> setHeightInMeters


I think the important thing to note is that it did work with the dot notation. I experienced the same thing.

Question: could it be that Apple is forcing our hand in ObjectiveC 2.0 to use dot notation if you use @property/@synthesis?


OK Nevermind!! Typos were the problem. There are no small typos in programming.


Also received this message. I note that when we are setting these variable names we use setHeightInMeters function though variable is heightInMeters, etc. We then dump that in favour of @property and @sythesize which saves having to get and set. So far so good. Then set variable in external main.m for bmi calculation and this problem occurs . If the dot notation is used, person.heightInMeters = 1.8;, all is good, note that it uses the variable name. Though am coming from c# where dot notation everywhere, am trying to do this properly so tried square bracket notation. [person heightInMeters:1.8]; seems fair to me, setting variable name as with dot notation, but fails with above message. [person setheightInMeters:1.8] odd to retain the set but new language, fails with above message. [person setHeightInMeters:1.8]; compiles however. So though the variable we are trying to set has a name beginning in lowercase, when using the bracket notation for setting the value we have to use set before the variable name and capitalise the first letter of the variable name. Does @property and @synthesize invoke some sort of naming convention along these lines? Are values better set using the functions first described in the chapter? Hope this helps others receiving the message. Would welcome comment from folks who know what this thing is doing.