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
Moose
  • 1,270
  • 2
  • 19
  • 33

2 Answers2

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.

Jef
  • 2,134
  • 15
  • 17
  • 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) – Moose Nov 11 '11 at 18:11
  • That should be another question. Answering it here would make it off-topic. (Something to get you going: http://stackoverflow.com/questions/6385212/how-does-the-new-automatic-reference-counting-mechanism-work) Also, if it works then accept one of the two answers. – Jef Nov 11 '11 at 18:14
  • 1
    @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 Nov 11 '11 at 18:27
  • @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. – Moose Nov 11 '11 at 19:11
2

Where did you allocate the thingy?
You need something like

mythingy = [[thingy alloc] init];
// or
mythingy = [thingy new];
filipe
  • 3,370
  • 1
  • 16
  • 22