read-write, , . , , .
Person:
#import <Foundation/Foundation.h>
@interface Person : NSObject {
@private
NSString *name_;
NSDate *dateOfBirth_;
}
@property (readonly, copy) NSString *name;
@property (readonly, copy) NSDate *dateOfBirth;
- (id)initWithName:(NSString *)name dateOfBirth:(NSDate *)dateOfBirth;
@end
#import "Person.h"
@implementation Person
@synthesize name = name_;
@synthesize dateOfBirth = dateOfBirth_;
- (id)initWithName:(NSString *)name dateOfBirth:(NSDate *)dateOfBirth {
self = [super init];
if (self) {
name_ = [name copy];
dateOfBirth_ = [dateOfBirth copy];
}
return self;
}
- (void)dealloc {
[name_ release];
[dateOfBirth_ release];
[super dealloc];
}
@end
-, , Person.m, name dateOfBirth readwrite. , ; , - .
, , . . ( , Mac OS X iOS, #) , .
, copy, . . -, , MutablePerson. ! , copy , , - name dateOfBirth . , -initWithName:dateOfBirth:, , ; . -, NSString, NSDate ; , , - . ( NSDate, , - ...)
, , . , , - .
. , , -isEqual: -hash , , NSCopying. :
@interface Person (ImmutableValueClass) <NSCopying>
@end
@implementation Person (ImmutableValueClass)
- (NSUInteger)hash {
return [name_ hash];
}
- (BOOL)isEqual:(id)other {
Person *otherPerson = other;
return ([super isEqual:otherPerson]
|| ([object isKindOfClass:[self class]]
&& [self.name isEqual:otherPerson.name]
&& [self.dateOfBirth isEqual:otherPerson.dateOfBirth]));
}
- (id)copyWithZone:(NSZone *)zone {
return [self retain];
}
@end
, , , , , @interface @implementation. , -hash -isEqual:, , NSObject. , , -copyWithZone: , self, , .
Core Data, , ; Core Data , -hash -isEqual:. NSCopying Core Data NSManagedObject; , "" , Core Data, , , .