I have a hierarchy of model objects which I will be displaying on different type of UITableViewCell subclasses. All decision is made on the fly as to which model object should be used and corresponding UITableViewCell subclass’ object is spawned and then set the model object to the UITableViewCell’s subclass object so that it can fetch values from it.
My UITableViewCell hierarchy is something like this:
The base class Cell hierarchy:
@interface BaseCell : UITableViewCell
{
Base *baseObj_;
}
@end
The subclass of cell hierarchy:
@interface DerivedCell : BaseCell
{
}
@property (nonatomic, retain) Derived *derivedObject;
@end
@implementation DerivedCell
@synthesize derivedObject = baseObj_;
@end
The base class of Model object:
@interface Base : NSObject
{
NSString *title_;
}
@property (nonatomic, retain) NSString *title;
@end
The subclass of model hierarchy
@interface Derived : Base
{
NSString *detailedText_;
}
@property (nonatomic, retain) NSString *detailedText;
@end
When I do so, I am having errors in this line:
@synthesize derivedObject = baseObj_;
Which reads:
-
Property ‘derivedObject’ attempting to use ivar ‘baseObj_’ declared in super class BaseCell.
-
Type of property ‘derivedObject’ (Derived*) does not match type of ivar ‘baseObj_’ (‘Base * __strong’)
I want to use properties and synthesize them so that I can leverage the uses of properties (like using dot notation etc.). I have for now used accessors and setters which solves the problem:
@interface DerivedCell : BaseCell
{
}
-(Derived*)derivedObject;
-(void)setDerivedObject:(Derived*)newDerivedObject;
@end
But I was just wondering if I could somehow fix these errors to use the properties only.
Thanks,
Raj
Try the below code I have modified your code a bit as shown below
Since you can assign class Base object to class Derived in @synthesize, it can be achieved by this way, I know you have tried it already, I have tried it with the below code and able to access the variables with dot, try the below code and let me know if it is not working
Another Solution which I have provided has an issue when I checked it
When I created instance for the class and as shown below
It didn’t assign the value to derivedTitle, If it is working for you please let me know
Another solution with memory referncing