Sen*_*ful 7 objective-c objective-c-category objective-c-protocol
我可以使用类别扩展UIView,但是它只适用于实现特定协议(WritableView)的子类吗?
即我可以做以下的事情吗?
@interface UIView<WritableView> (foo) // SYNTAX ERROR
- (void)setTextToDomainOfUrl:(NSString *)text;
- (void)setTextToIntegerValue:(NSInteger)value;
- (void)setCapitalizedText:(NSString *)text;
@end
@implementation UIView<WritableView> (foo)
// implementation of 3 above methods would go here
@end
Run Code Online (Sandbox Code Playgroud)
想象一下,我想将以下类别函数添加到任何实例UILabel:
[label setTextToDomainOfUrl:@"http://google.com"];
Run Code Online (Sandbox Code Playgroud)
这只是设置UILabel的text属性google.com.
同样地,我希望能够在其他几个类上调用此函数:
[button setTextToDomainOfUrl:@"http://apple.com"]; // same as: [button setTitle:@"apple.com" forState:UIControlStateNormal];
[textField setTextToDomainOfUrl:@"http://example.com"]; // same as: textField.text = @"example.com"
[tableViewCell setTextToDomainOfUrl:@"http://stackoverflow.com"]; // same as: tableViewCell.textLabel.text = @"stackoverflow.com"
Run Code Online (Sandbox Code Playgroud)
假设到目前为止我对我的设计非常满意,并且我希望为所有4个类增加2个方法:
[label setTextToIntegerValue:5] // same as: label.text = [NSString stringWithFormat:@"%d", 5];
[textField setCapitalizedText:@"abc"] // same as: textField.text = [@"abc" capitalizedString]
Run Code Online (Sandbox Code Playgroud)
所以现在我们有4个类,每个类有3个方法.如果我想实际完成这项工作,我需要编写12个函数(4*3).当我添加更多函数时,我需要在每个子类上实现它们,这很快就会变得非常难以维护.
相反,我想只实现一次这些方法,并简单地在受支持的组件上公开一个新的类别方法writeText:.这样,我可以将数字减少到4(每个支持的组件一个)+ 3(每个可用方法一个),总共需要实现7个方法.
注意:这些是愚蠢的方法,仅用于说明目的.重要的是,有许多方法(在这种情况下为3),不应该重复它们的代码.
我尝试实现这一点的第一步是注意到这4个类的第一个共同祖先是UIView.因此,放置3种方法的逻辑位置似乎属于以下类别UIView:
@interface UIView (foo)
- (void)setTextToDomainOfUrl:(NSString *)text;
- (void)setTextToIntegerValue:(NSInteger)value;
- (void)setCapitalizedText:(NSString *)text;
@end
@implementation UIView (foo)
- (void)setTextToDomainOfUrl:(NSString *)text {
text = [text stringByReplacingOccurrencesOfString:@"http://" withString:@""]; // just an example, obviously this can be improved
// ... implement more code to strip everything else out of the string
NSAssert([self conformsToProtocol:@protocol(WritableView)], @"Must conform to protocol");
[(id<WritableView>)self writeText:text];
}
- (void)setTextToIntegerValue:(NSInteger)value {
NSAssert([self conformsToProtocol:@protocol(WritableView)], @"Must conform to protocol");
[(id<WritableView>)self writeText:[NSString stringWithFormat:@"%d", value]];
}
- (void)setCapitalizedText:(NSString *)text {
NSAssert([self conformsToProtocol:@protocol(WritableView)], @"Must conform to protocol");
[(id<WritableView>)self writeText:[text capitalizedString]];
}
@end
Run Code Online (Sandbox Code Playgroud)
只要当前UIView实例符合WritableView协议,这三种方法就可以工作.所以我使用以下代码扩展了我支持的4个类:
@protocol WritableView <NSObject>
- (void)writeText:(NSString *)text;
@end
@interface UILabel (foo)<WritableView>
@end
@implementation UILabel (foo)
- (void)writeText:(NSString *)text {
self.text = text;
}
@end
@interface UIButton (foo)<WritableView>
@end
@implementation UIButton (foo)
- (void)writeText:(NSString *)text {
[self setTitle:text forState:UIControlStateNormal];
}
@end
// similar code for UITextField and UITableViewCell omitted
Run Code Online (Sandbox Code Playgroud)
现在,当我打电话给以下时:
[label setTextToDomainOfUrl:@"http://apple.com"];
[tableViewCell setCapitalizedText:@"hello"];
Run Code Online (Sandbox Code Playgroud)
有用!Hazzah!一切都很完美...直到我尝试这个:
[slider setTextToDomainOfUrl:@"http://apple.com"];
Run Code Online (Sandbox Code Playgroud)
代码编译(自UISlider继承UIView),但在运行时失败(因为UISlider不符合WritableView协议).
我真正想要做的是使这3种方法仅适用于那些writeText:实现了方法的UIViews(即那些实现WritableView我设置的协议的UIViews ).理想情况下,我会在UIView上定义我的类别,如下所示:
@interface UIView<WritableView> (foo) // SYNTAX ERROR
- (void)setTextToDomainOfUrl:(NSString *)text;
- (void)setTextToIntegerValue:(NSInteger)value;
- (void)setCapitalizedText:(NSString *)text;
@end
Run Code Online (Sandbox Code Playgroud)
这个想法是,如果这是有效的语法,它将[slider setTextToDomainOfUrl:@"http://apple.com"]在编译时失败(因为UISlider从未实现WritableView协议),但它会使我的所有其他示例成功.
所以我的问题是:有没有办法扩展一个类的类,但是只限于那些已经实现了特定协议的子类?
我意识到我可以将断言(它检查它符合协议)更改为if语句,但这仍然允许有缺陷的UISlider行进行编译.没错,它不会在运行时导致异常,但也不会导致任何事情发生,这也是我也试图避免的另一种错误.
类似的问题没有给出令人满意的答案:
这听起来像你在追求的是mixin:定义一系列形成你想要的行为的方法,然后将该行为仅添加到需要它的类集中.
下面是我用了很大的成功在我的项目的策略EnumeratorKit,增加了红宝石风格的块计数方法内置可可集合类(特别是EKEnumerable.h和EKEnumerable.m:
定义描述所需行为的协议.对于要提供的方法实现,将它们声明为@optional.
@protocol WritableView <NSObject>
- (void)writeText:(NSString *)text;
@optional
- (void)setTextToDomainOfUrl:(NSString *)text;
- (void)setTextToIntegerValue:(NSInteger)value;
- (void)setCapitalizedText:(NSString *)text;
@end
Run Code Online (Sandbox Code Playgroud)创建一个符合该协议的类,并实现所有可选方法:
@interface WritableView : NSObject <WritableView>
@end
@implementation WritableView
- (void)writeText:(NSString *)text
{
NSAssert(@"expected -writeText: to be implemented by %@", [self class]);
}
- (void)setTextToDomainOfUrl:(NSString *)text
{
// implementation will call [self writeText:text]
}
- (void)setTextToIntegerValue:(NSInteger)value
{
// implementation will call [self writeText:text]
}
- (void)setCapitalizedText:(NSString *)text
{
// implementation will call [self writeText:text]
}
@end
Run Code Online (Sandbox Code Playgroud)创建一个类别NSObject,可以在运行时将这些方法添加到任何其他类(请注意,此代码不支持类方法,只支持实例方法):
#import <objc/runtime.h>
@interface NSObject (IncludeWritableView)
+ (void)includeWritableView;
@end
@implementation
+ (void)includeWritableView
{
unsigned int methodCount;
Method *methods = class_copyMethodList([WritableView class], &methodCount);
for (int i = 0; i < methodCount; i++) {
SEL name = method_getName(methods[i]);
IMP imp = method_getImplementation(methods[i]);
const char *types = method_getTypeEncoding(methods[i]);
class_addMethod([self class], name, imp, types);
}
free(methods);
}
@end
Run Code Online (Sandbox Code Playgroud)现在,在要包含此行为的类中(例如,UILabel):
WritableView协议writeText:实例方法将其添加到您的实现的顶部:
@interface UILabel (WritableView) <WritableView>
@end
@implementation UILabel (WritableView)
+ (void)load
{
[self includeWritableView];
}
// implementation specific to UILabel
- (void)writeText:(NSString *)text
{
self.text = text;
}
@end
Run Code Online (Sandbox Code Playgroud)希望这可以帮助.我发现这是一种非常有效的方法来实现跨领域的关注点,而无需在多个类别之间复制和粘贴代码.
| 归档时间: |
|
| 查看次数: |
2157 次 |
| 最近记录: |