سؤال

Objective-с doesn't have built-in method visibility like protected, private, abstract. How to implement it like in Java?

Solution in my answer below

هل كانت مفيدة؟

المحلول

private method visibility - easily implemented inside .m files:

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

- (void)_somePrivateMethod;

@end

@implementation SomeClass

- (void)_somePrivateMethod
{
}

@end

protected and abstract method visibilities require Class Category, but it's not only one solution. You can use next model. For example you have abstract Data Model class which implements the basic mechanisms for working with some data, for example some synchronization. You need to create subclasses that will load the data in different ways. In java you can easily make it like this:

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;
    }

}

But you can't do the same in Objective-сб you need to use Class Categories, it no so cool :( . I found a way to do this in more elegant way using protocols:

In .h file:

    @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

In .m file:

#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
}

Sorry for bad english.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top