Objective C 中的大隐藏常数
我有一个很大的常量(一个具有 10^6 个值的 NSString)。由于它的大小,我想在源文件的末尾声明它(这样我就不必每次想编辑代码时都滚动它)。另外,由于它的大小,我希望它是一个常量,这样我可以在编译时而不是运行时加载它。另外,因为我不希望外部用户可以访问它,所以我不想在头文件中将其声明为 extern。
我使用实现文件中的下面的代码将其声明为常量,但是如果我将其移过实现的@end(出于明显的原因),它会给我一个“使用未声明的标识符'hugeConstantString'”。
NSString *const HugeConstantString = @"a_whooooooole_lotta_characters";
我已经检查过: Objective-C 中的常量 但它没有告诉我任何我不知道的事情。也许我的大脑已经崩溃了,但是:有什么方法可以在实现之后定义这个巨大的常量并且仍然可以访问它吗?或者,如果我在另一个头文件中声明它并导入它,其他人可以访问它吗?
谢谢!
I have a large constant (an NSString with 10^6 values). Because of its size I would like to declare it at the end of the source file (so I don't have to scroll through it every time I want to edit my code). Also because of its size I would like it to be a constant so I can load it at compile time instead of runtime. Also, because I do not want it accessible to outside users I do not want to declare it as extern in the header file.
I have it declared as a constant using the code below in the implementation file, however it is giving me a "Use of undeclared identifier 'hugeConstantString'" if I move it past the @end of the implementation (for obvious reasons).
NSString *const hugeConstantString = @"a_whooooooole_lotta_characters";
I've checked this out: Constants in Objective-C but it didn't tell me anything I didn't know already. Maybe my brain is fried, but: is there any way that I can define this huge constant AFTER my implementation and still have it accessible? Or if I declare it in another header file and import it, will it then be accessible to others?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这么大的字符串是个好主意,但是如果您要使用它,我建议将它放在自己的头文件中。
MyLongStringConstant.h
MyClass.h
如果您希望在应用程序的每个文件中都可以访问它,请在应用程序中导入标头
myApp_Prefix.pch
文件,该文件被导入到每个文件中。I'm not sure such a large string is a good idea, but if you're going to use it, I suggest putting it in its own header file.
MyLongStringConstant.h
MyClass.h
If you want to have it accessible in every file of your app, import the header inside your apps
myApp_Prefix.pch
file, which is imported into every file.我将保存“你为什么这样做”的对话,并为你发布一个简单的解决方案。感谢汤米的评论,这里是一个更简单的版本。
I am going to save the conversation of Why are you doing that and just post a simple solution for you. Thanks to Tommy in the comments here is a simpler version.