目标C - 如何使用外部变量?

时间:2022-09-07 21:04:16

I am trying to use extern variables.

我正在尝试使用extern变量。

It complains that because of using numberWithInt I am not passing a contants as the value of my variable

它抱怨因为使用numberWithInt我没有传递一个包含作为我变量的值

So I removed the const and it's complaining that an extern variable must be a constant, so what is the solutions here?

所以我删除了const并且它抱怨外部变量必须是常量,那么这里的解决方案是什么?

I DO NOT WANT TO USE INT

我不想使用INT

.h
extern NSNumber const *MoveID;

.m
NSNumber const *MoveID = [NSNumber numberWithInt:1];

4 个解决方案

#1


14  

You can try to do the following:

您可以尝试执行以下操作:

.h

extern NSNumber *MoveID;

.m

NSNumber *MoveID;
@implementation MYGreatClass
+ (void) initialize {
    static bool done = FALSE;
    if(!done){ // This method will be called again if you subclass the class and don't define a initialize method for the subclass
        MoveID = [[NSNumber numberWithInt:1] retain];
        done = TRUE;
    }
}

#2


3  

As @BoltClock said, you cannot set a non-constant value to be of const type.

正如@BoltClock所说,你不能将非常量值设置为const类型。

What you could do is this:

你能做的是:

extern NSNumber *MoveID;

And...

NSNumber *MoveID;
@implementation SomeClass 
static BOOL loaded = NO;
+ (void) initialize {
   if(!loaded) {
      MoveID = [[NSNumber alloc] initWithInt:1];
      loaded = YES;
   }
}
//blah blah blah

@end

#3


1  

EDIT: I just realized that I totally missed the question and was going on about why the error was occurring, oops. I'll leave the first part of my answer here though because Jacob Relkin quotes it in his answer.

编辑:我刚刚意识到我完全错过了这个问题,并且正在讨论错误发生的原因,哎呀。我会在这里留下我答案的第一部分,因为Jacob Relkin在他的回答中引用了它。


Because [NSNumber numberWithInt:1] is not a compile-time constant value, you cannot set an NSNumber created with it to a const variable.

因为[NSNumber numberWithInt:1]不是编译时常量值,所以不能将使用它创建的NSNumber设置为const变量。

There appears to be a radar about extern NSNumber consts, which seem to be unsupported in Objective-C. I guess you can use a preprocessor macro to create NSNumbers from constant ints or floats as described in this article. It's not nearly the same as what you intend but it seems to be pretty close.

似乎有关于外部NSNumber功能的雷达,在Objective-C中似乎没有支持。我猜你可以使用预处理器宏来创建来自常量int或浮点数的NSNumbers,如本文所述。它与你想要的几乎不一样,但似乎非常接近。

#4


0  

Just for completeness, the modern method is do do it as:

只是为了完整性,现代方法是这样做的:

in .h

extern NSNumber *MoveID;

in .m

NSNumber *MoveID;

...

- (void)viewDidLoad {
    [super viewDidLoad];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        MoveID = @1;
    });

    ...
}

dispatch_once() will only ever run once so the initialiser is not duplicated, and it is thread safe. Also, pushing down initialisation code lower down in the view lifecycle.

dispatch_once()只会运行一次,因此初始化程序不会重复,并且它是线程安全的。此外,在视图生命周期中将初始化代码降低。

#1


14  

You can try to do the following:

您可以尝试执行以下操作:

.h

extern NSNumber *MoveID;

.m

NSNumber *MoveID;
@implementation MYGreatClass
+ (void) initialize {
    static bool done = FALSE;
    if(!done){ // This method will be called again if you subclass the class and don't define a initialize method for the subclass
        MoveID = [[NSNumber numberWithInt:1] retain];
        done = TRUE;
    }
}

#2


3  

As @BoltClock said, you cannot set a non-constant value to be of const type.

正如@BoltClock所说,你不能将非常量值设置为const类型。

What you could do is this:

你能做的是:

extern NSNumber *MoveID;

And...

NSNumber *MoveID;
@implementation SomeClass 
static BOOL loaded = NO;
+ (void) initialize {
   if(!loaded) {
      MoveID = [[NSNumber alloc] initWithInt:1];
      loaded = YES;
   }
}
//blah blah blah

@end

#3


1  

EDIT: I just realized that I totally missed the question and was going on about why the error was occurring, oops. I'll leave the first part of my answer here though because Jacob Relkin quotes it in his answer.

编辑:我刚刚意识到我完全错过了这个问题,并且正在讨论错误发生的原因,哎呀。我会在这里留下我答案的第一部分,因为Jacob Relkin在他的回答中引用了它。


Because [NSNumber numberWithInt:1] is not a compile-time constant value, you cannot set an NSNumber created with it to a const variable.

因为[NSNumber numberWithInt:1]不是编译时常量值,所以不能将使用它创建的NSNumber设置为const变量。

There appears to be a radar about extern NSNumber consts, which seem to be unsupported in Objective-C. I guess you can use a preprocessor macro to create NSNumbers from constant ints or floats as described in this article. It's not nearly the same as what you intend but it seems to be pretty close.

似乎有关于外部NSNumber功能的雷达,在Objective-C中似乎没有支持。我猜你可以使用预处理器宏来创建来自常量int或浮点数的NSNumbers,如本文所述。它与你想要的几乎不一样,但似乎非常接近。

#4


0  

Just for completeness, the modern method is do do it as:

只是为了完整性,现代方法是这样做的:

in .h

extern NSNumber *MoveID;

in .m

NSNumber *MoveID;

...

- (void)viewDidLoad {
    [super viewDidLoad];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        MoveID = @1;
    });

    ...
}

dispatch_once() will only ever run once so the initialiser is not duplicated, and it is thread safe. Also, pushing down initialisation code lower down in the view lifecycle.

dispatch_once()只会运行一次,因此初始化程序不会重复,并且它是线程安全的。此外,在视图生命周期中将初始化代码降低。