我有以下协议:
@protocol MyProtocol
@property (nonatomic, retain) NSObject *myProtocolProperty;
-(void) myProtocolMethod;
@end
Run Code Online (Sandbox Code Playgroud)
我有以下课程:
@interface MyClass : NSObject {
}
@end
Run Code Online (Sandbox Code Playgroud)
我声明了一个类扩展,我必须在这里重新声明我的协议属性,否则我不能用我的其余类实现它们.
@interface()<MyProtocol>
@property (nonatomic, retain) NSObject *myExtensionProperty;
/*
* This redeclaration is required or my @synthesize myProtocolProperty fails
*/
@property (nonatomic, retain) NSObject *myProtocolProperty;
- (void) myExtensionMethod;
@end
@implementation MyClass
@synthesize myProtocolProperty = _myProtocolProperty;
@synthesize myExtensionProperty = _myExtensionProperty;
- (void) myProtocolMethod {
}
- (void) myExtensionMethod {
}
- (void) useMyConsumer {
[[[MyConsumer new] autorelease] consumeMyClassWithMyProtocol:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
MyConsumer只会从MyClass调用,所以我不希望任何其他类看到MyClass在MyProtocol上实现方法,因为它们不是公共API.同样,我不希望MyConsumer在MyClass中看到类扩展.
@interface MyConsumer : NSObject {
}
@end
@implementation MyConsumer
- (void) consumeMyClassWithMyProtocol: (MyClass<MyProtocol> *) myClassWithMyProtocol {
myClassWithMyProtocol.myProtocolProperty; // works, yay!
[myClassWithMyProtocol myProtocolMethod]; // works, yay!
myClassWithMyProtocol.myExtensionProperty; // compiler error, yay!
[myClassWithMyProtocol myExtensionMethod]; // compiler warning, yay!
}
@end
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以避免在我的类扩展中重新声明MyProtocol中的属性,以便私下实现MyProtocol?
您所称的"匿名类别"实际上称为类扩展,用于在实现文件中声明私有功能.最后一部分很重要,因为这意味着其他类将无法看到您放入类扩展中的声明(并且它们将无法看到您的类实现了这些方法MyProtocol).这也可能是在@synthesize没有重新声明属性的情况下失败的原因.
相反,在类的接口中声明您对协议的一致性,并添加您想要公开的任何方法:
@interface MyClass : NSObject <MyProtocol> {
}
// public methods and properties go here
@end
Run Code Online (Sandbox Code Playgroud)
如果将协议声明添加到接口,那么它也消除了使用者明确指定它的需要.您的使用者方法可以具有以下签名:
- (void) consumeMyClassWithMyProtocol: (MyClass *) myClassWithMyProtocol;
Run Code Online (Sandbox Code Playgroud)
编辑:听起来你正在寻找一种有选择地公开私有功能的方法.首先,我会尝试考虑一个不同的架构来实现你想要实现的目标,因为即将遵循的是一个相当不愉快的解决方案,如果一切都是公共的或私有的话,它通常会更好.
话虽如此,Apple通常通过为相关类提供单独的头文件来解决此问题,该头文件声明应该可见的方法.所以你会有你的类接口,在其中公开应该完全公开的所有内容:
// MyClass.h
@interface MyClass : NSObject {
}
@end
Run Code Online (Sandbox Code Playgroud)
还有一个单独的标题,您可以在其中声明伪私有内容的类别:
// MyClass+Private.h
#import "MyClass.h"
@interface MyClass (Private) <MyProtocol>
- (void)mySortaPrivateMethod;
@end
Run Code Online (Sandbox Code Playgroud)
MyClass.m 将实现这两个文件中的所有内容,并且仍然可以具有类扩展:
// MyClass.m
#import "MyClass.h"
#import "MyClass+Private.h"
@interface MyClass ()
- (void)myClassExtensionMethod;
@end
@implementation MyClass
// everything can go here
@end
Run Code Online (Sandbox Code Playgroud)
然后你的消费者将包括在内,MyClass+Private.h以便它可以看到那里的声明,而其他人只会使用MyClass.h.
| 归档时间: |
|
| 查看次数: |
370 次 |
| 最近记录: |