0

I am trying to get this code to work but cannot seem to get any output that shows the variables are being set:

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

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

@autoreleasepool {

thingy *mythingy;

    [mythingy setMass:75.00];
    [mythingy setTime:5.00];

    NSLog(@"mass of mythingy = %f kg", [mythingy mass]);
    NSLog(@"time of mythingy = %f sec", [mythingy time]);

    }
return 0;
}

this is the output i am getting:

mass of mythingy = 0.000000 kg
time of mythingy = 0.000000 sec

I also tried not using the @autoreleasepool (ARC) and the code looks like the following but with the same output as before:

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

    [mythingy setMass:75.00];
    [mythingy setTime:5.00];

    NSLog(@"mass of mythingy = %f kg", [mythingy mass]);
    NSLog(@"time of mythingy = %f sec", [mythingy time]);

[mythingy release];
[pool drain];

UPDATE:

okay so i took the previous code and added a line it looks like this now and works but is frustrating because i want to use ARC!!!!

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

    [mythingy setMass:75.00];
    [mythingy setTime:5.00];

    NSLog(@"mass of mythingy = %f kg", [mythingy mass]);
    NSLog(@"time of mythingy = %f sec", [mythingy time]);

[mythingy release];
[pool drain];

Output of this code:

mass of mythingy = 75.000000 kg
time of mythingy = 5.000000 sec
0

2 Answers 2

5

You have to create an object (mythingy) before you can use it. Try to change thingy *mythingy; into thingy *mythingy = [mythingy new]; Or initialize it with your custom method if you implemented one.

Sign up to request clarification or add additional context in comments.

4 Comments

yeah it works now, tottally works, but i want to be able to use that new Automatic Reference counting, i cannot seem to find any good resource on that subject so that @autoreleasepool can be used and memory management can be ignored (for simple projects)
That should be another question. Answering it here would make it off-topic. (Something to get you going: stackoverflow.com/questions/6385212/…) Also, if it works then accept one of the two answers.
@Mooseman - this is nothing to do with ARC- you need to alloc and init your object either way. ARC doesn't magically initialise objects for you.
@jrturton - I had read some documentation (very small amount) earlier and was under the impression that the ARC does indeed magically initialize and release objects. The reason why this was a significant belief in my mind was further strengthened by the fact that when I have ARC turned on and attempt to alloc or init or release or copy or new at any point it spits an error at me saying that im not allowed to do it.
2

Where did you allocate the thingy?
You need something like

mythingy = [[thingy alloc] init];
// or
mythingy = [thingy new];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.