@implementation 中声明的变量 [英] variables declared in @implementation

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

问题描述

我正在处理一本书中的代码清单,它有一对变量(特别是 NSString *)在@implementation 中而不是在@interface 中声明和初始化,但在任何方法体之外.我以前从未见过这个,我想知道这在范围等方面有什么不同.

I'm working through a code listing from a book and it has a pair of variables (specifically NSString *) declared and initialised in the @implementation rather than the @interface but outside of any method body. I've not seen this before and I'm wondering what the difference this makes in scope and so on.

我已经快速浏览了 The Objective C Programming Language,但我看不到任何描述其效果的内容.

I've had a quick look in The Objective C Programming Language but I can't see anything describing what effect this has.

谢谢

安迪

推荐答案

@implementation 内部声明的变量具有全局作用域.

Variables declared inside @implementation have global scope.

如果您将它们声明为静态",则它们仅在同一源文件中的方法中可见.

If you declare them as "static", they are only visible from the methods in the same source file.

所以:

@implementation MyClass

NSString *myString; // global scope, and accessible by all code in your project

@implementation MyClass

static NSString *myString; // global scope, but only accessible by code 
                           // in this source file

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

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