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.