-1

EDIT TO ADD: SOLVED AT BOTTOM, see main.m

I wanted to try the FizzBuzz Challenge in Objective-C since I'm teaching myself the language. I easily coded it in main.m, but I wondered if there was a way to code it via a header and implementation file. For my main.m version, I have:

#import <Foundation/Foundation.h>

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

        for (int i = 1; i <= 100; i++) {

            if (i % 3 == 0 && i % 5 == 0) {
                NSLog(@"FizzBuzz");
            }

            else  if (i % 3 == 0) {
                NSLog(@"Fizz");
            }

            else if (i % 5 == 0) {
                NSLog(@"Buzz");
            }

            else {
                NSLog(@"%d", i);
            }

        }

    }
    return 0;
}

I want to print the solution via a header and implementation file instead, but I'm not sure how to call the method in main.m (Just want to print the solution in various ways to practice objective-c's syntax and call rules).

For the .h, .m, and main.m, I have this so far:

.h

#import <Foundation/Foundation.h>

@interface FizzBuzz : NSObject

-(void)fizzBuzzCalc;

@end 

.m

#import "FizzBuzz.h"

@implementation FizzBuzz

-(void)fizzBuzzCalc
{
for (int i = 1; i <= 100; i++) {

    if (i % 3 == 0 && i % 5 == 0) {
        NSLog(@"FizzBuzz");
    }

    else  if (i % 3 == 0) {
        NSLog(@"Fizz");
    }

    else if (i % 5 == 0) {
        NSLog(@"Buzz");
    }

    else {
        NSLog(@"%d", i);
    }

}

}

@end

main.m

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

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

        FizzBuzz *test = [FizzBuzz new];
        [test fizzBuzzCalc];

        NSLog(@"%@", test);
    }
    return 0;
}

Not sure how to call the fizzBuzz method in main.m, which is my question.

I'm new to Objective-C, so this may seem like a dumb question, but I'm still learning.

EDIT TO ADD DISCUSSION

Not sure why, but I deleted the instance/method calling code in main.m and rewrote it back in the same way and Xcode stopped throwing errors. I don't know why, but I notice Xcode will sometimes throw errors for no reason and if you simply delete the code and rewrite it back in, Xcode will read it without a problem.

Yistorian
  • 49
  • 10
  • Why don't you want to use NSLog in your *.m file? And how stringWithFormat will provoke a spaghetti code? – FreeNickname Feb 21 '15 at 08:24
  • NSLog is implemented in my .m file already. I simply copy and pasted my main.m into my .m file as the `-*(void)FizzBuzz{}` method. I'm just not sure how to use it in main.m. That's what I'm trying to figure out. Just looking for different ways to implement the solution. `stringWithFormat` for this would be verbose for no reason, is what I'm saying. I know there is a simpler way, but I'm not sure how to go about it, regarding obj-c's syntax. – Yistorian Feb 21 '15 at 08:27
  • Well, you have a class that declares a `FizzBuzzz` method. You can just call it from main.m, can't you? Something like `void main() { MyClass *fizzBuzzInstance = [MyClass new]; [fizzBuzzInstance FizzBuzz]; return 0; }` – FreeNickname Feb 21 '15 at 08:43
  • I added more code for clarification. I'm not sure how to call the method in main.m. I attempted something along the lines of what you posted, but main.m is stating that the class and the FizzBuzz method are undeclared. – Yistorian Feb 21 '15 at 08:59
  • Had you import .h file? Add this line in `main.m`: `#import: "FizzBuzz.h"` or the .h file you had named. – Zigii Wong Feb 21 '15 at 09:03
  • No clue why I forgot to #import, but I corrected that, but still can't call the method in main.m. It now recognizes the class, but not the method. Not sure why. – Yistorian Feb 21 '15 at 09:13

1 Answers1

0

Thx to @FreeNickname and @Wongzigii for pointing out some simple mistakes in my code, I was able to determine that in order to call the method in main, I simply had to write the following code:

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

    FizzBuzz *test = [FizzBuzz new];
    [test fizzBuzzCalc];

    NSLog(@"%@", test);
}
return 0;
}

The reason why I wanted to rewrite the solution utilizing a .h and .m file was because writing methods in main.m is not best practice in objective-c development, so I thought this would be a good exercise.

Thanks again!

Yistorian
  • 49
  • 10