在Objective-C中定义全局可访问的字符串 [英] Defining a globally accessible string in Objective-C

查看:83
本文介绍了在Objective-C中定义全局可访问的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义全局可访问字符串的最佳方法是什么?

What is the best way to define a globally accessible string?

我看到整数通常是这样的
#define easy 0

I see that for integer it's usually like this #define easy 0

然而,我可以效仿NSString吗?

我试过static NSString * BACKGROUND = @bg.png;

工作,它会发出警告说变量从未被使用过。 (我把所有这些放在.h文件中)

However, how can I emulate that for NSString?
I tried static NSString *BACKGROUND = @"bg.png";
While that work, it does give a warning saying the variable is never used. (I have all these in a .h file)

NSString * const BACKGROUND = @bg.png;

Doing NSString *const BACKGROUND = @"bg.png"; is even worse since it says duplicate variable when I import the file.

我看到 #define BACKGROUND @bg.png更糟糕的是它在我导入文件时显示重复变量。 / code>似乎也可以工作。

I see that #define BACKGROUND @"bg.png" seems to work too.

所以我想什么时候使用 #define const & static

So I guess what is the difference between when to use #define, const & static

感谢,

Tee

Thanks,
Tee

推荐答案

这是做到这一点的正确方法。制作一些新的空白.h文件和.m。在你的.h文件中:

This is the correct way to do it. Make some new blank .h file and .m. In your .h file:

extern NSString* const BACKGROUND;

在您的.m文件中:

NSString* const BACKGROUND = @"bg.png";

这篇关于在Objective-C中定义全局可访问的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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