相关疑难解决方法(0)

在NSObject上使用类别提供默认协议实现有什么问题?

我一直在寻找一种方法来使用可选的协议方法并拥有干净的代码.换句话说:
1:respondsToSelector:我的代码没有调用
2.应该适用于任何方法签名,因此NSObject上的类别方法进行检查和调用performSelector:(NSInvocation与ARC合作有问题)
3:此解决方案,IMO,假装是普遍的但具有1的所有缺点

我最终提出了这个想法:

@protocol MyProtocol <NSObject>
@optional
-(void)optionalMethod;
@end

@interface ClassA : NSObject <MyProtocol>
@end

@implementation ClassA

-(void)optionalMethod{
     NSLog(@"ClassA implements optionalMethod");
}

@end

@interface ClassB : NSObject <MyProtocol>
@end

@implementation ClassB
//classB does not implement optionalMethod
@end

@interface NSObject (DefaultMyProtocolImplementation)
-(void)optionalMethod;
@end

@implementation NSObject (DefaultMyProtocolImplementation)
-(void)optionalMethod{
     NSLog(@"%@ does not implement optionalMethod", NSStringFromClass([self class]));
}
@end
Run Code Online (Sandbox Code Playgroud)

它似乎工作,即:

...
ClassA *objA = [[ClassA alloc] init];
ClassB *objB = [[ClassB alloc] init];

[objA optionalMethod]; //prints …
Run Code Online (Sandbox Code Playgroud)

protocols objective-c objective-c-runtime ios objective-c-category

8
推荐指数
1
解决办法
1080
查看次数

为在Objective-C中实现特定协议的类创建类别?

简短的问题描述

我可以使用类别扩展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]; …
Run Code Online (Sandbox Code Playgroud)

objective-c objective-c-category objective-c-protocol

7
推荐指数
1
解决办法
2157
查看次数