How can I solve this Problem with bidirectional dependencies in Objective-C classes?(如何解决 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
然后是ClassB的头部.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'之前的语法错误".当我删除用于导入 ClassA 的 import 语句时,删除 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 类中的双向依赖问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何解决 Objective-C 类中的双向依赖问题?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01