English 中文(简体)
转向超级阶级的方法不多
原标题:About moving few methods to the superclass

I need to move the same method from 4 different classes to the superclass. Such methods are exactly the same except for the type of a variable declared in them:

例如,在第一类方法中,

FirstClass var = [[FirstClass alloc] init]

第二类

SecondClass var = [[SecondClass alloc] init]

等等。

如何以最佳方式在超级阶级中实施这一变化?

Should I use NSClassFromString in the superclass and get each string from each method in the subclasses?

thanks

最佳回答

如果你打算从下级开始使用所有不同类型的iVar,那么我就把这一类放在超级市场,或者仅仅把它作为补贴储存。 之后,在您的每个子类别中都设置了一名财产入口,使IVar像你需要的那样投下。

@interface superClass : NSObject{
    id _superIvar;
}
@end

@implementation superClass : NSObject
    ....super s code....
@end

现在,在执行子类时,宣布属于以下类别的财产(或如果你想要公开的话,在接口中)。

@interface subClass (private)
@property (strong) ClassType *superIvar;
@end;

@implementation  
- (void) setSuperIvar:(ClassType *)superIvar{
    _superIvar = superIvar;
}
- (ClassType *) superIvar{
    return (ClassType *) _superIvar;
}

- (void) someMethodThatUsesSuperIvar{
    [self.superIvar doSomething];
}
@end

或者,如果你不希望打开你的话,那么你就会per。 我总是可以直接进入,你可以把财产放在上层,并通过下级的财产进入。 但这样,你可以很容易地获得超额的 cast。

问题回答

我确信,我知道你指的是什么。 因此,我可以回答错误的问题。

如果在您的班子内,你需要使用物体(我叫下工)做你的工作,但直到后来才知道该物体的类别,你可以使用依赖注射。

<>MyClass.h

@interface MyClass : NSObject

@property (nonatomic, retain) id<WorkerInterface> worker;

@end

<>MyClass.m

@implementation MyClass

@synthesize worker = _worker;

- (void)myMethod;
{
    [self.worker doSomething];
}

// You could also provide a default class to use if one is not passed in
//
// - (id<WorkerInterface)worker;
// {
//     if (!_worker) {
//         _worker = [[DefaultWorker alloc] init];
//     }
//     return _worker;
// }

@end

现在,每当我直截了这个类别时,我就能够简单地在适当的用途上通过,例如:

MyWorkerClass *worker = [[MyWorkerClass alloc] init]; // <- Conforms to @protocol(WorkerInterface)
MyClass *instance = [[MyClass alloc] init];
instance.worker = worker;

[instance doSomething];




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签