Note that there are some explanatory texts on larger screens.

plurals
  1. POConfused why one piece of code works and another doesn't in Objective C
    primarykey
    data
    text
    <p>Sorry about the title being extremely vague, I'm new to Objective C and struggling a little with it. Basically I have the following section of code:</p> <pre><code>Graph *graph1 = [[Graph alloc] init]; [graph1 addNode:@"TEST"]; </code></pre> <p>which is working to a degree. But I want to change it because the above code happens on a button press, and therefore I assume I am creating a new "*graph1" every time I do this. I thought I could simply change it to this: </p> <pre><code>if(self = [super init]) { [self setGraph: [[Graph alloc] init]]; } return self; </code></pre> <p>Where the above is in the init method, and below is the modified function:</p> <pre><code>[graph addNode:@"TEST"]; </code></pre> <p>However when debugging I've found addNode method is never called when it's like this.</p> <p>Thanks Zac</p> <p>This is testViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class Graph; @class Node; @interface testViewController : UIViewController { Graph *graph; UILabel *label; } @property (nonatomic, retain) IBOutlet UILabel *label; @property (nonatomic, retain) Graph *graph; - (IBAction) buttonPressed:(id)sender; @end </code></pre> <p>This is textViewController.m</p> <pre><code>#import "testViewController.h" #import "Graph.h" @implementation testViewController @synthesize label, graph; - (id)init { if(self = [super init]) { [self setGraph: [[Graph alloc] init]]; } return self; } - (IBAction)buttonPressed:(id)sender { //Graph *graph1 = [[Graph alloc] init]; [graph addNode:@"TEST"]; Node *node1 = [[Node alloc] initWithLabel: @"LABEL"]; label.text = node1.label; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload