【问题标题】:Elegant way to create protected/abstract/etc methods in Objective-c [closed]在 Objective-c 中创建受保护/抽象/等方法的优雅方法 [关闭]
【发布时间】:2014-04-22 16:32:22
【问题描述】:

Objective-с 没有像 protectedprivateabstract 这样的内置方法可见性。 如何像 Java 一样实现它?

下面我的答案中的解决方案

【问题讨论】:

标签: ios objective-c methods abstract protected


【解决方案1】:

私有方法可见性 - 在 .m 文件中轻松实现:

@interface SomeClass () <SomeProtocolForPrivateImplementation>
{
    int _someVar1;
    int _someVar2;
    int _someVarN;
}
@property (nonatomic, readwrite) NSString *someProperty;

- (void)_somePrivateMethod;

@end

@implementation SomeClass

- (void)_somePrivateMethod
{
}

@end

protectedabstract 方法可见性需要 Class Category,但它不仅仅是一种解决方案。您可以使用下一个模型。例如,您有抽象的数据模型类,它实现了处理一些数据的基本机制,例如一些同步。您需要创建以不同方式加载数据的子类。在java中你可以很容易地做到这一点:

public abstract class AbstractDataModel
{
    public abstract String getDataModelID();

    protected void syncData()
    {
    // ... some data synchronization
    }
}
public class DataModel extends AbstractDataModel
{

    @Override
    public String getDataModelID() {
        // TODO Auto-generated method stub
        return null;
    }

}

但是你不能在 Objective-сб 中做同样的事情,你需要使用 Class Categories,它没有那么酷:(。 我找到了一种使用协议以更优雅的方式执行此操作的方法:

在.h文件中:

    @protocol ModelSubclassingProtocol;
    @interface Model : NSObject
    {
        int _someVar;
    }

    @property (nonatomic, readonly) NSString *someProperty;

    - (id)initWithDictionary:(NSDictionary*)info;
    - (BOOL)updateFromDictionary:(NSDictionary*)info; // return TRUE if update has changes
    - (void)reloadItems;

    @end



    // use protocol for subclasses
    @protocol ModelSubclassingProtocol <NSObject>

    @required
    // all abstract methods here
    - (Class)someAbstractMethod1;
    + (NSString*)someAbstractMethod2;
    - (BOOL)someAbstractMethod3;

@optional
// all protected methods here
- (void)someProtectedMethod1;
- (BOOL)someProtectedMethod2;

    @end

在 .m 文件中:

#define SUBCLASS ((id<ModelSubclassingProtocol>)self)

@interface Model () <SomeProtocolsForPrivateImplementation>
{
    NSMutableArray  *_somePrivateVar1;
}
@property (nonatomic, readwrite) NSString *someProperty1;

@end

@implementation RDModel

+ (id)alloc
{
    // check subclassing protocol implementation
    Protocol    *protocol       = @protocol(ModelSubclassingProtocol);

    NSString    *protocolName   = NSStringFromProtocol(protocol);
    NSString    *className      = NSStringFromClass(self.class);

    NSAssert([self.class conformsToProtocol:protocol], @"\n\nERROR: \"%@\" must be conforms to protocol \"%@\".\nRecommended to use internal class extension in \"%@.m\", sample code:\n\n@interface %@ () <%@>\n@end\n\n", className, protocolName, className, className, protocolName);

    return [super alloc];
}

#pragma mark - Private

- (NSDictionary*)_synchronizeItems:(NSArray *)array
{
//... some implementation before

// use abstract methods here
        NSString *itemIdentifier = [SUBCLASS itemIdentifier];

//... some implementation after
}

抱歉英语不好。

【讨论】:

  • 我看不出您的方法受到何种保护?
  • 您可以使用仅由子类实现的协议来保护方法的可见性。
  • 这种方法在抽象方法上的问题是这些方法和抽象超类之间没有强链接。如果您真的想模仿其他语言并在任何这些抽象方法上使用多态性,您将“需要”协议类型的对象:超类本身不实现协议并且根本不引用这些方法。我写 "need" 是因为您可以实际上调用任何对象的任何方法,因为 Objective-C 使用“消息发送”而不是“方法调用”模式。
  • 抽象类可以在 private.h 中私下实现协议,然后子类可以在其 .m 中导入私有头。然后子类可以调用超方法。
猜你喜欢
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 2017-06-14
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多