Objective C - 如何使用extern变量? [英] Objective C - How to use extern variables?

查看:133
本文介绍了Objective C - 如何使用extern变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用extern变量。

它抱怨说因为使用了numberWithInt,我没有将一个contants作为我的变量值

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

我不想使用INT

  .h 
extern NSNumber const * MoveID;

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


解决方案

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



.h

  extern NSNumber * MoveID; 

.m

  NSNumber * MoveID; 
@implementation MYGreatClass
+(void)初始化{
static bool done = FALSE;
if(!done){//如果你继承了这个类的子类并且没有为这个子类定义一个初始化方法,这个方法将会被再次调用
MoveID = [[NSNumber numberWithInt:1] retain];
完成= TRUE;
}
}


I am trying to use extern variables.

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

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

I DO NOT WANT TO USE INT

.h
extern NSNumber const *MoveID;

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

解决方案

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;
    }
}

这篇关于Objective C - 如何使用extern变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆