如何在 Objective-c 中将数组声明为常量?
以下代码给了我错误:
// constants.h
extern NSArray const *testArray;
// constants.m
NSArray const *testArray = [NSArray arrayWithObjects: @"foo", @"bar", nil];
我得到的错误是初始化元素不是常量
或者,如果我拿走指针指示符 (*),我会得到:Objective-C 类“NSArray”的静态分配实例
The following code is giving me errors:
// constants.h
extern NSArray const *testArray;
// constants.m
NSArray const *testArray = [NSArray arrayWithObjects: @"foo", @"bar", nil];
The error I get isinitializer element is not constant
Or if I take away the pointer indicator (*) I get:statically allocated instance of Objective-C class 'NSArray'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简而言之,你不能。 Objective-C 对象(NSString 除外)仅在运行时创建。因此,您不能使用表达式来初始化它们。
有几种方法。
(1) 声明不带
const
关键字的NSArray *testArray
,然后使用一些代码来设置在应用程序生命周期早期调用的值。(2) 声明一个返回数组的方便的类方法,然后在该方法中使用
static NSArray *myArray
并将其视为单例(在 SO 中搜索“objective-c singleton”大约无数次)关于如何实例化的答案)。In short, you can't. Objective-C objects are, with the exception of NSString, only ever created at runtime. Thus, you can't use an expression to initialize them.
There are a handful of approaches.
(1) Declare
NSArray *testArray
without theconst
keyword and then have a bit of code that sets up the value that is invoked very early during application lifecycle.(2) Declare a convenient class method that returns the array, then use a
static NSArray *myArray
within that method and treat it as a singleton (search SO for "objective-c singleton" for about a zillion answers on how to instantiate).有点晚了,但由于您在程序过程中没有更改值,如果您只处理字符串,则可以通过使用
C
声明数组来执行以下操作数组:在您的
constants.h
文件中,然后在您的constants.m
文件中,您可以向其中添加对象,如下所示:然后要访问成员,您可以使用C
sizeof()
运算符执行 for 循环:这显然是一个
C
数组,而不是>NSArray
这样你就不会像objectAtIndex:
这样附加到它上面的所有有趣方法,所以你可以在程序中的某个地方创建一个辅助函数,使用它循环遍历所有字符串我上面概述的方法并返回一个NSArray
(甚至是NSMutableArray
)。但是,如果您正在做我所做的事情并且只需要在整个程序中使用一个NSString
值的常量数组,那么此方法效果最好。这样做会将所有字符串数组常量封装在
constants.h
中,并且通过在.pch< 中添加
constants.h
仍然可以在整个程序中使用。 /code> 文件,而不是仅仅为这个值数组创建一个单例或用一些代码设置该数组,这有点违背了constants
文件的目的,因为它从中删除了实际的常量。 code>constants
文件..根据 @JesseGumpo 的评论进行编辑:
由于使用
sizeof()
确定数组的大小可能存在问题,一个简单的方法解决方法是在常量文件中声明数组的大小,如下所示:然后要访问 for 循环中的成员,可以这样做:
是的,这不会动态捕获数组的大小,但如果您在常量文件中声明一个数组时,您从一开始就知道该数组的大小,因此即使它添加了两行代码,它仍然完成了在常量文件中包含数组的任务。
如果有人有更多建议或可能知道其他一些
C
技巧,请在下面发表评论!A little late to the party, but since you're not changing the values through the course of the program, if you were only dealing with strings, you could do the following by declaring your array using a
C
array:in your
constants.h
file, and then in yourconstants.m
you could add objects to it like so:Then to access a member, you could do a for loop like so with aC
sizeof()
operator:This obviously is a
C
array and not aNSArray
so you don't get all of the fun methods attached to it likeobjectAtIndex:
, so you could create a helper function somewhere in your program that loops through all of the strings using the method I outlined above and returns anNSArray
(orNSMutableArray
even). But, if you were doing what I am and just need a constant array ofNSString
values to use throughout your program, this method works the best.Doing it this way encapsulates all of your string array contants in
constants.h
, and is still available throughout your program by addingconstants.h
in your.pch
file instead of creating a singleton just for this array of values or setting the array with a little code, which sorta defeats the purpose of aconstants
file because it removes the actual constants out of theconstants
file..EDIT per @JesseGumpo's Comment:
Since there may be issues with using
sizeof()
to determine the size of the array, a simple workaround is to declare the size of the array in your constants file like so:And then to access the members in a for loop you can do so like this:
Yes, this doesn't dynamically capture the size of the array, but if you're declaring an array in a constants file you already know the size of that array from the start, so even though it adds two more lines of code, it still accomplishes the task of having an array in a constants file.
If anyone has any more suggestions or may know some other
C
tricks please leave a comment below!下面是一个宏,用于在方法范围内的静态实例的一行中执行此操作。
使用示例
Here's a macro to do it in one line for a static instance in a method scope.
Use example
这非常简单:
#define arrayTitle [NSArray arrayWithObjects: @"hi",@"foo",nil]
放在实现之前并且不带分号。
希望有帮助。
It's pretty easy :
#define arrayTitle [NSArray arrayWithObjects: @"hi",@"foo",nil]
put before implementation and without semicolon.
hope it helps.
对于我来说,对常量数组使用以下实现更方便
As for me, it is more convenient to use the following implementation for an array of constants
我有一个名为“Constants.h”的头文件,在下一个常量数组中:
基本上,当您在代码中调用 arrayOfStrings 时,会被替换为 @[@"1", @"2", @"3", @" 4"] 与 arraysOfIds 相同。
I have a header file called "Constants.h" and within the next constant arrays:
Basically, when you call arrayOfStrings in your code, is replaced with @[@"1", @"2", @"3", @"4"] and the same thing with arraysOfIds.