Tom
Tom

Reputation: 11

Unused Variable OSX

I am very new to programming, currently trying to progress through the Ray Wenderlich beginner tutorials, but I have fallen at the first hurdle! It appears, either I have set up my program wrong, or the tutorial is out of date as when I began the program we had different code, and when I try to run the code he has produced it came up with errors.

His code

// //  main.m //  Are You A WIZARD? //
#import <Foundation/Foundation.h>   int main (int argc, const char * argv[]) {

     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

     //These are the different variables that will be evaluated to generate responses. 
     float strength, intelligence, speed, alchemy_skill, sum, avg;

     //I included (1-10) so that users would know how to answer.
     NSLog(@"What is your strength (1-10)?");
     NSLog(@"What is your intelligence (1-10)?");
     NSLog(@"What is your speed (1-10)?");
     NSLog(@"What is your alchemy skill level (1-10)?");

     [pool drain];
     return 0; }

My code

#import <Foundation/Foundation.h>

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

    @autoreleasepool {

        //These are the different variables that will be evaluated to generate responses:
        float strength, intelligence, speed, alchemy_skill, sum, avg;

        //I included (1-10) so that users would know how to answer.
        NSLog(@"What is your strength (1-10)?");
        NSLog(@"What is your intelligence (1-10)?");
        NSLog(@"What is your speed (1-10)?");
        NSLog(@"What is your alchemy skill level (1-10)?");

    }
    return 0; }

When I run his code it has flat out errors such as "NSAutoreleasePool is not available in automatic reference counting mode", and when I run mine it says that I have "Unused variables".

Thanks for any help! I am sure I have made a silly mistake straight away, but some clarification would be most appreciated!

Upvotes: 1

Views: 335

Answers (2)

Liyan Chang
Liyan Chang

Reputation: 8051

You're right on both counts.

NSAutoreleasePool is not available in automatic reference counting mode

This is because you are running your code in a different mode then he is running his. Automatic reference counting (circa 2012) does the memory management automatically while before you had to manually keep track. Two possible recommendations:

  • Don't use ARC. Then his code will work out of the box.

You can do this by starting a new project and unchecking "use automatic reference counting" during the configuration stages.

  • Remove the autorelease code:

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
    
        //These are the different variables that will be evaluated to generate responses:
        float strength, intelligence, speed, alchemy_skill, sum, avg;
    
        //I included (1-10) so that users would know how to answer.
        NSLog(@"What is your strength (1-10)?");
        NSLog(@"What is your intelligence (1-10)?");
        NSLog(@"What is your speed (1-10)?");
        NSLog(@"What is your alchemy skill level (1-10)?");
        return 0;
    } 
    

I would recommend picking a more up-to-date tutorial or continue on with a no-arc project.

To your second question on unused variables, XCode is telling you that you have variables that you make but don't use. As you can see, you create strength et al and don't use them. It's not a problem. I'm assuming that you'll need to use them later on in the following steps.

Upvotes: 0

Linuxios
Linuxios

Reputation: 35783

You get the unused variables warning because of this line:

float strength, intelligence, speed, alchemy_skill, sum, avg;

You declare 6 variables here and never do anything about them. The compiler warns you about this because normally it's a problem, but if you're going to use them later, it's OK.

As for his code, it is outdated, and was made before the creation of something called ARC, which is a complex solution to an even more complex probelm.

I agree with @Mardin in the comments. Don't start programming with Obj C. I started after five or six other languages, and parts of it still give me headaches. It's a better second or third language.

Upvotes: 2

Related Questions