Objective C 中的大隐藏常数

发布于 2024-10-28 07:36:03 字数 575 浏览 3 评论 0原文

我有一个很大的常量(一个具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-11-04 07:36:03

我不确定这么大的字符串是个好主意,但是如果您要使用它,我建议将它放在自己的头文件中。

MyLongStringConstant.h

#define kLongString @"..."

MyClass.h

....
#import "MyLongStringConstant.h"
...
//Do something with kLongString
...

如果您希望在应用程序的每个文件中都可以访问它,请在应用程序中导入标头 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

#define kLongString @"..."

MyClass.h

....
#import "MyLongStringConstant.h"
...
//Do something with kLongString
...

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.

秋意浓 2024-11-04 07:36:03

我将保存“你为什么这样做”的对话,并为你发布一个简单的解决方案。感谢汤米的评论,这里是一个更简单的版本。

#import "LargeStringTest.h"


@implementation LargeStringTest

//Declare the string
static NSString *hugeConstantString;

- (id)init {
    self = [super init];
    if (self) {
        NSLog(@"Large String %@", hugeConstantString);
    }
    return self;
}

//Place all other code here

//Assign the string
static NSString *hugeConstantString = @"a_whooooooole_lotta_characters";

@end

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.

#import "LargeStringTest.h"


@implementation LargeStringTest

//Declare the string
static NSString *hugeConstantString;

- (id)init {
    self = [super init];
    if (self) {
        NSLog(@"Large String %@", hugeConstantString);
    }
    return self;
}

//Place all other code here

//Assign the string
static NSString *hugeConstantString = @"a_whooooooole_lotta_characters";

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文