Reputation: 3545
I am trying to create Mutable and Immutable classes.
There are Person protocol, Person class, and MutablePerson class.
I would like to create name property, and synthesize only getter for Person class, and setter for MutablePerson class. How can I do this?
@protocol Person <NSObject>
@property (nonatomic, readonly, copy) NSString *name;
@end
@interface Person : NSObject <Person>
@end
@interface MutablePerson : Person
@property (nonatomic, copy) NSString *name;
@end
There are errors when I try to synthesize setter in MutablePerson class. How can I synthesize only setter for property?
Upvotes: 0
Views: 93
Reputation: 838
Based on your example, I'm not sure why you are using a protocol to determine that you name of your Person when you are using inheritance to create your MutablePerson.
I've set up a basic Person and MutablePerson object using only inheritance, and it seems to work fine:
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, strong, readonly) NSString *name;
- (id)initWithName:(NSString*)name;
@end
@interface MutablePerson : Person
@property (nonatomic, strong, readwrite) NSString *name;
@end
Person.m
#import "Person.h"
@interface Person ()
@property (nonatomic, strong, readwrite) NSString *name;
@end
@implementation Person
- (id)initWithName:(NSString *)name {
if (self = [super init]) {
_name = name;
}
return self;
}
@end
@implementation MutablePerson
@end
Let me know if this isn't the intended behavior you have in mind, and I will edit my response to help you further if I can.
EDIT: Here is the sample code I used to create an example Person and MutablePerson:
Person *testPerson = [[Person alloc] initWithName:@"TestName"];
MutablePerson *testMutablePerson = [[MutablePerson alloc] init];
testMutablePerson.name = @"MutableName";
Upvotes: 1