Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Objective-C, how do you set up properties of properties, i.e. of a class set up as another class property?
    primarykey
    data
    text
    <p>I have been trying to set up an instance property of a class which is itself a class that contains instance variables. I would like to access these as properties. In C#, this is easy to do with the pre-set get and set variables:</p> <pre><code>public class TheOverallClass { private ClassName _propertyName; public ClassName PropertyName { get { return _propertyName; } set { _propertyName = value; } } </code></pre> <p>...and PropertyName being instantiated within the constructor:</p> <pre><code> public TheOverallClass() { PropertyName = new ClassName(); } } </code></pre> <p>Say PropertyName had an instance variable/property named ThisValue, I could then access it as:</p> <pre><code>TheOverallClass overallClass = new TheOverallClass(); overallClass.PropertyName.ThisValue = 20; // int Console.WriteLine(overallClass.PropertyName.ThisValue.ToString()); </code></pre> <p>How would I go about doing this in Objective-C? I have had a go, but have run into difficulty.</p> <p>The PropertyName equivalent is coded as:</p> <p>... in AnotherClass.h:</p> <pre><code>@interface AnotherClass : NSObject { int thisValue; } @property (readwrite,assign) int ThisValue; @end </code></pre> <p>... and in AnotherClass.m</p> <pre><code>@implementation AnotherClass @synthesize ThisValue=thisValue; @end </code></pre> <p>This is then used in the ClassName equivalent, MyClass:</p> <p>... in MyClass.h:</p> <pre><code>@class AnotherClass; @interface MyClass : NSObject { AnotherClass* another; } @property (nonatomic,retain) AnotherClass* Another; @end </code></pre> <p>... and in MyClass.m:</p> <pre><code>@implementation MyClass @synthesize Another=another; @end </code></pre> <p>When I try to get/set values within code, Xcode returns "Accessing unknown 'ThisValue' component of a property". The code I use to access is:</p> <pre><code>MyClass* me = [[MyClass alloc] init]; me.Another.ThisValue = 20; NSLog(@"Value = %i", me.Another.ThisValue); </code></pre> <p>What am I doing wrong?</p>
    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