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:
public class TheOverallClass
{
private ClassName _propertyName;
public ClassName PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
...and PropertyName being instantiated within the constructor:
public TheOverallClass()
{
PropertyName = new ClassName();
}
}
Say PropertyName had an instance variable/property named ThisValue, I could then access it as:
TheOverallClass overallClass = new TheOverallClass();
overallClass.PropertyName.ThisValue = 20; // int
Console.WriteLine(overallClass.PropertyName.ThisValue.ToString());
How would I go about doing this in Objective-C? I have had a go, but have run into difficulty.
The PropertyName equivalent is coded as:
... in AnotherClass.h:
@interface AnotherClass : NSObject {
int thisValue;
}
@property (readwrite,assign) int ThisValue;
@end
... and in AnotherClass.m
@implementation AnotherClass
@synthesize ThisValue=thisValue;
@end
This is then used in the ClassName equivalent, MyClass:
... in MyClass.h:
@class AnotherClass;
@interface MyClass : NSObject {
AnotherClass* another;
}
@property (nonatomic,retain) AnotherClass* Another;
@end
... and in MyClass.m:
@implementation MyClass
@synthesize Another=another;
@end
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:
MyClass* me = [[MyClass alloc] init];
me.Another.ThisValue = 20;
NSLog(@"Value = %i", me.Another.ThisValue);
What am I doing wrong?