我如何解决这个问题与双向依赖的Objective-C类? [英] How can I solve this Problem with bidirectional dependencies in Objective-C classes?

查看:137
本文介绍了我如何解决这个问题与双向依赖的Objective-C类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这可能是一个非常愚蠢的初学者问题,但是:

Okay, this might be a very silly beginner question, but:

我有一个ClassA,它将从ClassB创建一个子对象,一个实例变量。详细:ClassA将在指定的初始化器中分配和初始化ClassB,并将其分配给childObject实例变量。它的头像这样:

I've got an ClassA, which will create an child object from ClassB and assign that to an instance variable. In detail: ClassA will alloc and init ClassB in the designated initializer, and assign that to the childObject instance variable. It's header looks like this:

#import <Foundation/Foundation.h>
#import "ClassB.h"

@interface ClassA : NSObject {
    ClassB *childObject;
}

@end

的B类。 ClassB必须引用ClassA。

Then, there is the header of ClassB. ClassB has to have a reference to ClassA.

#import <Foundation/Foundation.h>
#import "ClassA.h"

@interface ClassB : NSObject {
    ClassA *parentObject;
}

- (id)initWithClassA:(ClassA*)newParentObject;

@end

当ClassA对象从ClassB创建一个子对象时,那么ClassA对象将调用ClassB对象的指定初始化器,它必须传递自身(self)。

When the ClassA object creates an child object from ClassB, then the ClassA object will call the designated initializer of the ClassB object, where it has to pass itself (self).

我觉得有什么问题,但现在我不知道它是什么。有人认为我知道这不行。他们不能进口彼此。编译器只是告诉我:错误:语法错误之前'ClassA'。当我删除import语句导入ClassA,删除ClassA * parentObject实例变量,并删除指定的初始化(获取ClassA引用),然后它工作。

I feel that there is something wrong, but for now I don't get exactly what it is. One think I know is that this does not work. They can't bove import eachother. The compiler just tells me: "error: syntax error before 'ClassA'". When I remove the import statement for importing ClassA, remove the ClassA *parentObject instance variable and remove the designates initializer (that takes ClassA reference), then it works.

现在,这里是我想实现的(如果这很重要):我需要一些非常特殊和复杂的行为在UIScrollView。所以我决定把它子类化。因为主要部分将在委托方法内部完成,所以我决定为我的UIScrollView子类创建一个通用委托对象。然后,我的UIScrollView子类自动创建该特殊委托对象并将其分配给其委托属性。委托对象本身需要引用它的父对象,即自定义的UIScrollView,以便它可以访问该滚动视图的子视图。但无论如何,即使有一个更好的方法来完成这个问题,我仍然想知道我如何能做到这一点与两个相互依赖的对象,需要彼此。

Now, here is what I want to achieve (if this matters): I need some very special and complex behavior in an UIScrollView. So I decided to subclass it. Because the main part is going to be done inside the delegate methods, I decided to create an "generic" delegate object for my subclass of UIScrollView. My UIScrollView subclass then creates automatically that special delegate object and assigns it to its delegate property. The delegate object itself needs a reference to it's parent, the customized UIScrollView, so that it has access to the subviews of that scroll view. But anyways, even if there's a better way to get this problem done, I'd still like to know how I could do it that way with two "interdependent" objects that need eachother.

欢迎任何建议!

推荐答案

您不需要在头文件中导入这些类。在头文件中使用 @class ,然后仅在.m文件中使用#import:

You don't need to import those classes in the header files. Use @class in the header files, and then use #import only in the .m files:

// ClassA.h:
@class ClassB;
@interface ClassA : NSObject
{
    ClassB *childObject;
}
@end


// ClassA.m:
#import "ClassA.h"
#import "ClassB.h"
@implementation ClassA
//..
@end





// ClassB.h
@class ClassA;
@interface ClassB : NSObject
{
    ClassA *parentObject;
}
- (id)initWithClassA:(ClassA*)newParentObject;
@end


// ClassB.m:
#import "ClassB.h"
#import "ClassA.h"
@implementation ClassB
//..
@end

这篇关于我如何解决这个问题与双向依赖的Objective-C类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆