请在目标C中解释Getter和Setters [英] Please explain Getter and Setters in Objective C

查看:61
本文介绍了请在目标C中解释Getter和Setters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Setters和Getters(Noobie)-iPhone SDK

Possible Duplicate:
Setters and Getters (Noobie) - iPhone SDK

我是这里的初学者.最近两个月我才刚刚开始学习iOS,并且我没有任何编程背景. (不过是Java的一点点).谁能解释一下Objective C中的getter和setter吗?它们有什么用?为什么我们使用@property@synthesize?

I am a beginner here. I have just started learning iOS for the last two months and I do not have any programming background. (Little bit of Java though). Can anyone please explain what is getter and setter in Objective C? What is the use of them? Why do we use @property and @synthesize?

推荐答案

Getter是一种方法,该方法在您每次访问属性(用@property声明)(从中读取值)时都会被调用.该方法返回的任何值均视为该属性的值:

Getter is a method which gets called every time you access (read value from) a property (declared with @property). Whatever that method returns is considered that property's value:

@property int someNumber;

...

- (int)someNumber {
    return 42;
}

...

NSLog("value = %d", anObject.someNumber); // prints "value = 42"

Setter是每次属性值更改时都会调用的方法.

Setter is a method which gets called every time property value is changed.

- (void)setSomeNumber: (int)newValue { // By naming convention, setter for `someValue` should 
                                    // be called `setSomeValue`. This is important!
    NSLog("someValue has been assigned a new value: %d", newValue);
}

...

anObject.someNumber = 19; // prints "someValue has been assigned a new value: 19"

通常,从getter返回相同的值并在setter中打印新的值通常没有多大意义.要实际存储某些内容,您必须在类中声明一个实例变量(ivar):

Usually it doesn't make much sense to just return the same value from getter and print new value in setter. To actually store something you have to declare an instance variable (ivar) in your class:

@interface SomeClass : NSObject {
    int _someNumber;
}

并使访问器(getter和setter的统称)存储/检索其值:

and make accessors (the collective name for getters and setters) to store/retrieve it's value:

- (int)someNumber {
    return _someNumber;
}

- (void)setSomeNumber:(int)newValue {
    _someNumber = newValue;
}

...

SomeClass *anObject = [[SomeClass alloc]init];
anObject.someNumber = 15;
NSLog(@"It's %d", anObject.someNumber); // prints "It's 15"

好吧,现在该属性的行为就像通常的变量一样.编写所有这些代码有什么意义?

Okay, now that property behaves just like the usual variable. What's the point in writing all that code?

首先,从现在开始,您可以向访问器中添加一些额外的代码,这些代码将在每次访问或更改属性时执行.这样做有多种原因,例如,我可能要进行某种隐藏的计算,或者更新对象的状态,缓存内容等.

First, from now on you can add some extra code to the accessors, which will get executed each time the property is accessed or changed. There are multiple reasons for doing that, for example I may want to do some kind of hidden calculations, or updating my object's state, caching stuff etc.

第二,可可中有很酷的机制,称为键值编码(KVC)和键值观察(KVO).它们取决于属性.您可以在开发人员库中阅读有关它们的信息: KVC编程指南《 KVO编程指南》 .这些都是高级主题.

Second, there are cool mechanisms called Key-Value Coding (KVC) and Key-Value Observing (KVO) in Cocoa. They depend on properties. You can read about them in the Developer Library: KVC Programming Guide and KVO Programming Guide. Those are advanced topics though.

最后,在目标C中没有为对象进行静态分配.所有对象都是动态分配的(原因 ).如果要将对象指针保留在实例变量(而不是属性)中,则每次将新值分配给ivar时,都必须手动执行所有内存管理(当

Last, in Objective C there is no static allocation for objects. All the objects are dynamically allocated (reason). If you want to keep your object pointers in instance variables (as opposed to properties) you will have to do all the memory management manually every time you assign new value to your ivar (not true when Automatic Reference Counting is on). Using properties you could put some memory management code in the accessors and make your life easier.

我不认为这种解释对不熟悉Objective C内存管理的人来说没有多大意义,因此,要么阅读一些真正的文档/教程,要么仅使用属性(而不是实例变量),直到您以一种或另一种方式学习所有细节.就个人而言,我不喜欢第二种选择,但这取决于您.

I don't believe this explanation will make much sense to someone who is not familiar with Objective C memory management, so, either read some real docs/tutorials on it, or just use properties (instead of instance variables) until you learn all the details one way or another. Personally, I don't like the second option, but it's up to you.

您可以使用@synthesize使编译器自动为您生成基本访问器和基础实例变量.除了上面的代码(-(int)someNumber-(void)setSomeNumber:),您可以编写

You can use @synthesize to make the compiler generate basic accessors and underlying instance variables for you automatically. Instead of the code above (-(int)someNumber and -(void)setSomeNumber:) you could just write

@synthesize someNumber = _someNumber; // = _someNumbers tells compiler 
                                      // to name the instance variable `_someNumber`. 
                                      // You could replace it with = `_somethingElse`, of
                                      // course, but that's an ill idea.

这一行为您生成int _someNumber变量,someNumber getter和setSomeNumber setter.如果您希望访问器做的事情不只是从某些实例变量中存储/检索值,还需要自己编写.

This single line generates int _someNumber variable, someNumber getter and setSomeNumber setter for you. If you want the accessors to do anything more complex than just store/retrieve the value from some instance variable, you will have to write them yourself.

希望所有这些都有意义.

Hope all this makes any sense.

这篇关于请在目标C中解释Getter和Setters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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