Skip navigation.
 
mlStrange problem when not declaring the functions in interface
FROM : Pradeep Kumar
DATE : Mon Apr 18 22:08:51 2005

Hi All

I am not sure if I am missing something really fundamental or it is really a bug. But I have
this strange problem that's occurring when functions are not declared in the interface.
Thought of broadcasting this incase some one has any insight on why this is happening.

Please review the following code snippet. Assume that the functions foo1 and foo2 are
declared in the interface declaration of MyObject.

@implementation MyObject

-(void)awakeFromNib
{
   NSLog(@"[self foo1] returned %d", [self foo1]);
   NSLog(@"[self foo2] returned %d", [self foo2]);
   int foo1 = [self foo1];
   int foo2 = [self foo2];
   NSLog(@"Variables foo1 = %d\tfoo2 = %d", foo1, foo2);
   
   int i = 100;
   NSLog(@"i = %d", i);
   
   i = i+[self foo1];
   NSLog(@"Executing i = i+[self foo1] = %d", i);
   i = i+[self foo2];
   NSLog(@"Executing i = i+[self foo2]; = %d", i);
}

-(int)foo1
{
   return 10;
}

-(int)foo2
{
   return 20;
}

@end

The result you get in the log is

[self foo1] returned 10
[self foo2] returned 20
Variables foo1 = 10    foo2 = 20
i = 100
Executing i = i+[self foo1] = 110
Executing i = i+[self foo2]; = 130

The results are perfect.

Now remove the declarations of foo1 and foo2 from the interface file of MyObject. After
doing this here's what I get.

[self foo1] returned 10
[self foo2] returned 20
Variables foo1 = 10    foo2 = 20
i = 100
Executing i = i+[self foo1] = 410
Executing i = i+[self foo2]; = 1660

See the results of the last two statements. 410 and 1660. Why is this happening?

I am using XCode 1.5 with  Component versions Xcode IDE: 389.0, Xcode Core: 387.0,
ToolSupport: 372.0 on 10.3.9 (7W98). I know it is recommended that functions be declared
in the interface. But can not declaring the functions cause such a huge difference in ways
you can use non-declared functions?

Thanks
prady

Related mailsAuthorDate
mlStrange problem when not declaring the functions in interface Pradeep Kumar Apr 18, 22:08
mlRe: Strange problem when not declaring the functions in interface Shawn Erickson Apr 18, 22:34
mlRe: Strange problem when not declaring the functions in interface Ivan S. Kourtev Apr 18, 22:36
mlRe: Strange problem when not declaring the functions in interface Daniel DeCovnick Apr 19, 05:34