解决 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
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangcbjfi
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22