Objective-C实例变量? [英] Objective-C instance variables?

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

问题描述

我敢肯定,我的困惑仅仅是因为被困在"Java思维模式"中,而不是理解Obj-C在这种情况下的不同之处.

I'm sure my confusion here is just a result of being stuck in a "Java mindset" and not understanding how Obj-C differs in this case.

在Java中,我可以像这样在类中声明一个变量,并且该类的每个 instance 都有自己的变量:

In Java, I can declare a variable in a class, like this, and each instance of that class will have it's own:

MyClass {

  String myVar;

  MyClass() {
    // constructor
  }
}

在Obj-C中,我试图通过只在.m文件中声明一个变量来做相同的事情,如下所示:

In Obj-C I tried to do the same thing by declaring a variable only in the .m file like this:

#import "MyClass.h"

@implementation MyClass

NSString *testVar;

@end

我在这里的期望是此变量的作用域仅限于此类.因此,我创建了第二个类(相同):

My expectation here was that this variable has a scope limited to this class. So I created a second class (identical):

#import "MySecondClass.h"

@implementation MySecondClass

NSString *testVar;

@end

我所看到的(让我感到困惑)是,在一个类中更改变量会影响在另一类中看到的值.实际上,如果我设置一个断点,然后对该变量跳转到定义",它将带我进入

What I'm seeing (and has me baffled) is that changing the variable in one class, affects the value seen in the other class. In fact, if I set a breakpoint, and then "Jump to Definition" of the variable, it takes me to th

我创建了一个非常小的Xcode项目,该项目在此处.

I've created an extremely small Xcode project that demonstrates the problem here.

推荐答案

更改此内容:

@implementation MyClass

NSString *testVar;

@end

收件人:

@implementation MyClass {
    NSString *testVar;
}

// methods go here

@end

您将得到预期的结果.

正如您所拥有的,实际上是在创建全局变量.链接程序将这两个全局变量合并为一个,这就是为什么在设置一个时都将其更改的原因.花括号中的变量将是适当的(并且是私有的)实例变量.

As you had it, you are actually creating a global variable. The two global variables were combined into one by the linker which is why both changed when you set one. The variable in curly braces will be a proper (and private) instance variable.

在没有明显理由被否决之后,我想我会指出旧的"做事方式和新的方式.

After being downvoted for no apparent reason, I thought I'd point out the "old" way of doing things, and the new way.

旧方法:

SomeClass.h

SomeClass.h

@interface SomeClass : UIViewController <UITextFieldDelegate> {
    UITextField *_textField;
    BOOL _someBool;
}

@property (nonatomic, assign) BOOL someBool;

// a few method declarations

@end

SomeClass.m

SomeClass.m

@implementation SomeClass

@synthesize someBool = _someBool;

// the method implementations

@end

现在使用现代的Objective-C编译器进行新的改进:

Now the new and improved way with the modern Objective-C compiler:

SomeClass.h

SomeClass.h

@interface SomeClass : UIViewController

@property (nonatomic, assign) BOOL someBool;

// a few method declarations

@end

SomeClass.m

SomeClass.m

@interface SomeClass () <UITextFieldDelegate>
@end

@implementation SomeClass {
    UITextField *_textField;
}

// the method implementations

@end

新方法具有多个优点.主要优点是,有关该类的实现的特定详细信息都不会出现在.h文件中.客户无需知道什么代表了实现需求.客户无需知道我使用的是什么.现在,如果实现需要新的ivar或需要使用新的协议,则.h文件不会更改.这意味着更少的代码被重新编译.它更清洁,效率更高.它还使编辑变得更容易.当我编辑.m文件并意识到我需要一个新的ivar时,请在我已经在编辑的同一.m文件中进行更改.无需来回交换.

The new way has several advantages. The primary advantage is that none of the implementation specific details about the class appear in the .h file. A client has no need to know what delegates the implementation needs. The client has no need to know what ivars I use. Now, if the implementation needs a new ivar or it needs to use a new protocol, the .h file doesn't change. This mean less code gets recompiled. It cleaner and much more efficient. It also makes for easier editing. When I'm editing the .m file and realize I need a new ivar, make the change in the same .m file I'm already editing. No need to swap back and forth.

还请注意,实现不再需要该属性的ivar或@synthesize.

Also note the implementation no longer needs an ivar or @synthesize for the property.

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

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