[英]Objective-C Mutable subclass pattern?
在Objective-C中是否有用于实现可变/不可变对象类对的标准模式? 我目前有类似以下内容,我根据此链接编写
不可变类:
@interface MyObject : NSObject <NSMutableCopying> {
NSString *_value;
}
@property (nonatomic, readonly, strong) NSString *value;
- (instancetype)initWithValue:(NSString *)value;
@end
@implementation MyObject
@synthesize value = _value;
- (instancetype)initWithValue:(NSString *)value {
self = [self init];
if (self) {
_value = value;
}
return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return [[MyMutableObject allocWithZone:zone] initWithValue:self.value];
}
@end
可变类:
@interface MyMutableObject : MyObject
@property (nonatomic, readwrite, strong) NSString *value;
@end
@implementation MyMutableObject
@dynamic value;
- (void)setValue:(NSString *)value {
_value = value;
}
@end
这有效,但它暴露了iVar。 是否有更好的实施可以解决这种情况?
您的解决方案遵循一个非常好的模式:可变类不会从其基础复制任何内容,并且在不存储任何其他状态的情况下公开其他功能。
这有效,但它暴露了iVar。
由于实例变量默认情况下是@protected
,因此公开的_value
仅对继承MyObject
的类可见。 这是一个很好的权衡,因为它可以帮助您避免数据重复,而不会公开暴露用于存储对象状态的数据成员。
是否有更好的实施可以解决这种情况?
在类扩展中声明value
属性。 扩展名类似于没有名称的类别,但必须是类实现的一部分。 在MyMutableObject.m文件中,执行以下操作:
@interface MyMutableObject ()
@property(nonatomic, readwrite, strong) value
@end
现在您已经声明了您的属性,但它只在您的实现中可见。
dasblinkenlight的答案是正确的。 问题中提供的模式很好。 我提供了两种不同的替代方案。 首先,以可变类中未使用的iVar为代价,属性是原子的。 其次,与许多基础类一样,不可变实例的副本只返回self。
MyObject.h:
@interface MyObject : NSObject <NSCopying, NSMutableCopying>
@property (atomic, readonly, copy) NSString *value;
- (instancetype)initWithValue:(NSString *)value NS_DESIGNATED_INITIALIZER;
@end
MyObject.m
#import "MyObject.h"
#import "MyMutableObject.h"
@implementation MyObject
- (instancetype)init {
return [self initWithValue:nil];
}
- (instancetype)initWithValue:(NSString *)value {
self = [super init];
if (self) {
_value = [value copy];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
// Do not use the iVar here or anywhere else.
// This pattern requires always using self.value instead of _value (except in the initializer).
return [[MyMutableObject allocWithZone:zone] initWithValue:self.value];
}
@end
MyMutableObject.h:
#import "MyObject.h"
@interface MyMutableObject : MyObject
@property (atomic, copy) NSString *value;
@end
MyMutableObject.m:
#import "MyMutableObject.h"
@implementation MyMutableObject
@synthesize value = _value; // This is not the same iVar as in the superclass.
- (instancetype)initWithValue:(NSString *)value {
// Pass nil in order to not use the iVar in the parent.
// This is reasonably safe because this method has been declared with NS_DESIGNATED_INITIALIZER.
self = [super initWithValue:nil];
if (self) {
_value = [value copy];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone {
// The mutable class really does need to copy, unlike super.
return [[MyObject allocWithZone:zone] initWithValue:self.value];
}
@end
测试代码片段:
NSMutableString *string = [NSMutableString stringWithString:@"one"];
MyObject *object = [[MyObject alloc] initWithValue:string];
[string appendString:@" two"];
NSLog(@"object: %@", object.value);
MyObject *other = [object copy];
NSAssert(object == other, @"These should be identical.");
MyMutableObject *mutable1 = [object mutableCopy];
mutable1.value = string;
[string appendString:@" three"];
NSLog(@"object: %@", object.value);
NSLog(@"mutable: %@", mutable1.value);
在上一行之后的一些调试:
2017-12-15 21:51:20.800641-0500 MyApp[6855:2709614] object: one
2017-12-15 21:51:20.801423-0500 MyApp[6855:2709614] object: one
2017-12-15 21:51:20.801515-0500 MyApp[6855:2709614] mutable: one two
(lldb) po mutable1->_value
one two
(lldb) po ((MyObject *)mutable1)->_value
nil
正如评论中所提到的,这需要在基类中使用getter而不是iVar。 许多人会认为这是一件好事,但这场辩论在这里是偏离主题的。
您可能会注意到的一个小差异是我已经使用了属性的copy属性。 这可以变得强大,而代码变化很小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.