在目标c中声明全局变量的问题 [英] problem with declare a global variable in objective c

查看:83
本文介绍了在目标c中声明全局变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类的1中有一个全局变量,例如 classA.h

i have a global variable in 1 of the class like classA.h

const NSString *global;

classA.m

global=[array objectAtIndex:0];//a array store sort of string

在另一个类中,我想调用此全局变量

and in another class i want to call this global variable

classB.m

import "class.h"

NSLog(@"%@",global);

但是它不起作用,我知道当我直接向全局变量而不是从另一个变量赋值时,它将起作用,但是有人可以告诉我如何从数组中实现它吗?

but it doesnt work,i know when i jus assigned a value directly to my global variable instead of from another variable it will work but can somebody show me how to make it achieve from a array?

推荐答案

在标题中,使用:

extern const NSString *global;

以及实施(.m):

const NSString *global;

外部"引用告诉所有包含文件的变量存在,但在其他地方声明.最后,在实现文件中,放入实际的声明.

The "extern" reference tells all including files that the variable exists, but it's declared elsewhere. Finally, in your implementation file, you put the actual declaration.

这篇关于在目标c中声明全局变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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