I'm trying to create a method that uses a static global variable to keep track of how many times a certain method is called, (in this case an add method to add 2 fractions).

I have the following code.

Code:

```
[B][I]// Fraction.h[/I][/B]
+(int)countAdd;
+(Fraction *)addCount: (Fraction *)f;
-(Fraction *)add: (Fraction *)f;
[B][I]// Fraction.m[/I][/B]
[B]+(Fraction *)addCount: (Fraction *)f {
extern int gAddCounter;
gAddCounter += 1;
[I]return [f add: [Fraction self]];[/I] // This is wrong
}
[/B]
+(int)countAdd {
extern int gAddCounter;
return gAddCounter;
}
-(Fraction *)add: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
[B][I]// main.m[/I][/B]
Fraction *myFraction = [[Fraction alloc] init];
Fraction *myFractionB = [[Fraction alloc] init];
Fraction *resultFraction;
// Set fraction to 1/3
[myFraction setNum: 1 setDi: 4];
[myFractionB setNum: 6 setDi: 4];
// Display the fraction
printf("%i/%i + %i/%i is: \n", myFraction.numerator, myFraction.denominator, myFractionB.numerator,
myFractionB.denominator);
resultFraction = [myFraction addCount: myFractionB];
[resultFraction printReduced: YES];
```

If anyone can help me understand and give me a hint on how to fix it, that would be greatly appreciated

Here will be my entire code:

**Fraction.h**

Code:

```
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
@property int numerator, denominator;
-(void)printReduced: (bool)b;
-(void)setNum: (int)n setDi: (int)d;
-(void)reduce;
+(int)countFractions;
+(int)countAdd;
-(double)convertToNum;
+(Fraction *)allocF;
+(Fraction *)addCount: (Fraction *)f;
-(Fraction *)add: (Fraction *)f;
-(Fraction *)subtract: (Fraction*)f;
-(Fraction *)multiply: (Fraction *)f;
-(Fraction *)divide: (Fraction *)f;
@end
```

**Fraction.m**

Code:

```
#import "Fraction.h"
static int gCounter, gAddCounter;
@implementation Fraction
@synthesize numerator, denominator; // Name the variables numerator and denominator, instead of _numerator and _denominator, like the @property does automatically
+(Fraction *)allocF {
extern int gCounter;
gCounter += 1;
return [Fraction alloc];
}
+(int)countFractions {
extern int gCounter;
return gCounter;
}
+(Fraction *)addCount: (Fraction *)f {
extern int gAddCounter;
gAddCounter += 1;
return [f add: [Fraction self]];
}
+(int)countAdd {
extern int gAddCounter;
return gAddCounter;
}
-(void)printReduced: (bool)b {
if (b == YES) {
[self reduce];
}
if (numerator >= denominator) {
int properNumerator = numerator % denominator;
int wholeNumber = numerator / denominator;
if (properNumerator == 0) {
printf("%i\n", wholeNumber);
} else {
printf("%i %i/%i\n", wholeNumber, properNumerator, denominator);
}
} else {
printf("%i/%i\n", numerator, denominator);
}
}
-(void)setNum: (int)n setDi: (int)d {
numerator = n;
denominator = d;
}
-(void)reduce {
int u = numerator, v = denominator, temp;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
-(double)convertToNum {
if (denominator != 0) {
return (double)numerator / denominator;
} else {
return NAN;
}
}
-(Fraction *)add: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
-(Fraction *)subtract: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator - denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
-(Fraction *)multiply: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
-(Fraction *)divide: (Fraction *)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denominator;
result.denominator = denominator * f.numerator;
return result;
}
@end
```

**main.m**

Code:

```
#import <Foundation/Foundation.h>
#import "Fraction.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Allocatiing fractions
Fraction *myFraction = [[Fraction alloc] init];
Fraction *myFractionB = [[Fraction alloc] init];
// Allocating fractions using a custom alloc method
Fraction *countFractionA = [[Fraction allocF] init];
Fraction *countFractionB = [[Fraction allocF] init];
Fraction *resultFraction;
// Set fraction to 1/3
[myFraction setNum: 1 setDi: 4];
[myFractionB setNum: 6 setDi: 4];
// Display the fraction
printf("%i/%i + %i/%i is: \n", myFraction.numerator, myFraction.denominator, myFractionB.numerator,
myFractionB.denominator);
resultFraction = [myFraction add: myFractionB];
[resultFraction printReduced: YES];
resultFraction = [myFraction addCount: myFractionB];
[resultFraction printReduced: NO];
resultFraction = [myFraction multiply: myFractionB];
//[resultFraction printReduced: YES];
resultFraction = [myFraction divide: myFractionB];
//[resultFraction printReduced: YES];
printf("Fractions allocated: %i\n", [Fraction countFractions]);
}
return 0;
}
```