Objective C 静态类变量
我是 Objective C 的新手,正在阅读 Steven Holzner 所著的《Visual Quickstart Guide: Objective-C》一书,Peachpit Press
在第 6 章:面向对象编程中,有一个名为“使用类变量”的部分,他在其中写道:
您可以创建类变量以供使用 你的课程,但有一个问题:每个对象 该类的共享相同的变量,所以 如果一个对象改变了一个类变量,那么 所有对象的变量都会更改。你创造 带有 static 关键字的类变量。 类变量通常很有用:例如, 您可以使用类变量来跟踪 创建的特定类的对象数量 在一个程序中。您将在此任务中执行此操作。
并要求输入以下代码:
#import <stdio.h>
#import <Foundation/NSObject.h>
@interface TheClass: NSObject
static int count; //error: cannot declare variable inside @interface or @protocol
+(int) getCount;
@end
...
此代码在 Xcode 4 中给我一个错误:
无法在@interface或@protocol内声明变量
这本书是错误的还是我做错了什么?
I am new to Objective C and am reading a book called "Visual Quickstart Guide: Objective-C" by Steven Holzner, Peachpit Press
In Chapter 6: Object Oriented Programming, there is a section called Using Class Variables where he writes:
You can create class variables for use with
your classes, but there’s a hitch: every object
of that class shares the same variable, so
if one object changes a class variable, that
variable is changed for all objects. You create
class variables with the static keyword.
Class variables are often useful: for example,
you can use a class variable to keep track of
the number of objects of a particular class created
in a program. You’ll do that in this task.
And says to enter the following code:
#import <stdio.h>
#import <Foundation/NSObject.h>
@interface TheClass: NSObject
static int count; //error: cannot declare variable inside @interface or @protocol
+(int) getCount;
@end
...
This code gives me an error in Xcode 4:
Cannot declare variable inside @interface or @protocol
Is the book wrong or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在实现文件(
.m
文件)中声明静态变量。这应该有效:它本身不是一个类变量; Objective-C 没有类变量的概念。然而,与检索该变量的类方法相结合,它的功能与类变量类似。然而,它实际上只是一个可由类的实现访问的 C 静态变量。
You declare the static variable in the implementation file (
.m
file). This should work:It's not a class variable per se; Objective-C has no notion of a class variable. However, coupled with the class method for retrieving this variable, it functions similarly to a class variable. However, it's really just a C static variable that's accessible by the class's implementation.
我看过一本关于 Unix 的可视化快速入门指南,它让我很费劲。这一款似乎并没有好多少,至少从样本来看是这样。在 Objective-C 中创建类变量的正确方法是这样的:
这实际上是一个静态变量,而不是真正的类变量。但无论如何你都不应该太担心类变量,它们通常表明你做错了什么。 (我有点过于简单化了,但没有太多。)
如果您正在寻找一本像样的 Objective-C 书籍,请阅读 Apple 的。它是免费的,而且是一本很好的读物。
I’ve seen one Visual Quickstart Guide about Unix and it sucked big time. This one does not seem to be much better, at least from the sample. The correct way to create a class variable in Objective-C looks like this:
This is in fact a static variable, not a true class variable. But you should not worry about class variables too much anyway, they’re usually a sign that you’re doing something wrong. (I’m oversimplifying a bit, but not much.)
If you’re looking for a decent Objective-C book, read the one by Apple. It’s free and it’s a good reading.
如果“类变量”需要的不仅仅是简单的初始化,请使用dispatch_once:
If the "class variable" needs more than trivial initialization, use
dispatch_once
:您应该在放置 @implementation 的 .m 文件中声明该变量。因此,
值得注意的是 Objective-C 实际上并不支持类变量。但是您可以使用静态变量来模拟它们,就像我们在这里所做的那样。
You should declare the variable in the .m file, where the @implementation is put. So,
It is important to note that Objective-C does not actually support class variables. But you can simulate them with static variables, as we are doing here.