访问其他项目中 class 的私有变量
Access private variables of a class in other project
我有一个巨大的 Objective-C 项目,我想在一个 WorkSpace 中拆分成多个项目。最初我做到了,代码编译得很好,但我决定也将一个大类 class 移动到其他项目,现在链接器不了解情况:
Undefined symbols for architecture x86_64:
"_OBJC_IVAR_$_MyClass.data", referenced from:
-[MyClass(Viz) someTask] in MyClass+Viz.o
d: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
简而言之,我的工作区如下所示:
- MyLib project:
MyClass.h
MyClass.m
MyClass+Inner.h
MyClass+Inner.m
- MyApp project:
MyClass+Viz.h
MyClass+Viz.m
MyLib 编译成MyLib.framework
,在MyApp 中使用。正如我所说,在将 MyClass+Viz
移动到 MyApp 项目之前,所有复杂的逻辑都正常工作,因此项目链接可能没有问题。另外,我检查了两次,确保所有文件在 Build Phases 设置部分都被正确标记。
这是 MyClass.h
文件:
@interface MyClass : NSObject
- (instancetype)init;
- (void)public_methods;
@end
这是 MyClass+Inner.h
文件:
#import "MyClass.h"
@interface MyClass ()
{
// Variables placed here
// so that only class methods can access them
SomeStruct* data;
// other fields ...
}
@end
@interface MyClass (Inner)
- (void)private_methods;
@end
和 MyClass+Viz.m
文件:
#import "MyClass+Viz.h"
#import "MyClass+Inner.h"
@implementation MyClass (Viz)
- (int)someTask {
return data->length;
// this or any other stuff with
// class variables from MyClass+Inner.h
// leads to "Undefined symbols" by linker
}
@end
如何让它工作,让链接器看到 class' 来自其他项目的私有变量?
首先,您通常应该使用 @property
而不是实例变量。
其次,您可以使用多个 header 文件和 class 扩展来执行类似的操作。
通常,
MyClass.h
MyClass_Internal.h
MyClass.m
MyClass_Internal.m(通常不使用)
MyClass.m 将导入两个 header。 MyClass.h 将定义 public 接口,而 MyClass_Internal.h 将有一个 class 扩展来提供私有接口:
@interface MyClass()
@property(...) Thing *internalThing;
@end
我有一个巨大的 Objective-C 项目,我想在一个 WorkSpace 中拆分成多个项目。最初我做到了,代码编译得很好,但我决定也将一个大类 class 移动到其他项目,现在链接器不了解情况:
Undefined symbols for architecture x86_64:
"_OBJC_IVAR_$_MyClass.data", referenced from:
-[MyClass(Viz) someTask] in MyClass+Viz.o
d: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
简而言之,我的工作区如下所示:
- MyLib project:
MyClass.h
MyClass.m
MyClass+Inner.h
MyClass+Inner.m
- MyApp project:
MyClass+Viz.h
MyClass+Viz.m
MyLib 编译成MyLib.framework
,在MyApp 中使用。正如我所说,在将 MyClass+Viz
移动到 MyApp 项目之前,所有复杂的逻辑都正常工作,因此项目链接可能没有问题。另外,我检查了两次,确保所有文件在 Build Phases 设置部分都被正确标记。
这是 MyClass.h
文件:
@interface MyClass : NSObject
- (instancetype)init;
- (void)public_methods;
@end
这是 MyClass+Inner.h
文件:
#import "MyClass.h"
@interface MyClass ()
{
// Variables placed here
// so that only class methods can access them
SomeStruct* data;
// other fields ...
}
@end
@interface MyClass (Inner)
- (void)private_methods;
@end
和 MyClass+Viz.m
文件:
#import "MyClass+Viz.h"
#import "MyClass+Inner.h"
@implementation MyClass (Viz)
- (int)someTask {
return data->length;
// this or any other stuff with
// class variables from MyClass+Inner.h
// leads to "Undefined symbols" by linker
}
@end
如何让它工作,让链接器看到 class' 来自其他项目的私有变量?
首先,您通常应该使用 @property
而不是实例变量。
其次,您可以使用多个 header 文件和 class 扩展来执行类似的操作。
通常,
MyClass.h MyClass_Internal.h MyClass.m MyClass_Internal.m(通常不使用)
MyClass.m 将导入两个 header。 MyClass.h 将定义 public 接口,而 MyClass_Internal.h 将有一个 class 扩展来提供私有接口:
@interface MyClass()
@property(...) Thing *internalThing;
@end