Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to make <code>measureType</code> an instance variable, so that each object of this type that you create has its own copy:</p> <pre><code>@interface ObjectSelectionViewController : NSViewController { NSString * measureType; // Declare an NSString instance variable } - (id) initWithMeasureType: (int)value; @end </code></pre> <p>As it is, there is only one copy of the variable, and every time you instantiate a new object, its value changes. Since each instance is referring to the same copy, they all get the same value:</p> <pre><code>ObjectSelectionViewController *length = [[ObjectSelectionViewController alloc] initWithMeasureType:0]; NSLog(@"%@", [length measurementType]); // Prints "Length" ObjectSelectionViewController *mass = [[ObjectSelectionViewController alloc] initWithMeasureType:1]; NSLog(@"%@", [length measurementType]); // Prints "Mass" </code></pre> <p>You also need to change your <code>init...</code> method as mentioned by other answerers:</p> <pre><code>- (id) initWithMeasureType: (int)value { // Call superclass's initializer self = [super init]; if( !self ) return nil; switch (value) { case 0: // Length measureType = @"Length"; break; case 1: // Mass measureType = @"Mass"; break; case 2: // Volume measureType = @"Volume"; break; } return self; } </code></pre> <p>Since you are assigning a literal string to the instance variable, you do not need to worry about managing its memory; if you were doing anything more complicated, you would probably do well by declaring a <a href="http://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/DeclaredProperty.html#//apple_ref/doc/uid/TP40008195-CH13-SW1">property</a>. Another note: initializer methods should always return <code>id</code>, a generic object pointer, to allow subclasses to work properly.</p>
 

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