自变量和变量之间的区别 [英] Difference between self.variable and variable

查看:358
本文介绍了自变量和变量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
类属性mVar与实例变量self.mVar之间的区别

Possible Duplicate:
Difference between class property mVar and instance variable self.mVar

我对使用Objective-C进行开发是陌生的,我无法完全弄清以下内容之间的区别:

I am new to developing in Objective-C and I couldn't quite figure out what the difference is between the following:

首先让我解释一下我的情况.我有一个NSMutableArray,并在我的.h文件中创建了该文件并将其输出.现在,当我将数组分配为

First let me explain my situation. I've got an NSMutableArray, and I created and outlet for it in my .h file. Now when I assign an array to it as

self.myMutableArray=myArray

我得到一个错误;但是只是

I get an error; However just

myMutableArray=myArray

正常工作.

我对解决错误不感兴趣.我只想知道将self放在某物前面有什么区别?为什么我也可以不使用self来使用该变量,以及它带来了哪些限制?

I am not interested in the resolving of the error. I just want to know what is the difference when putting self in front of something? And why I am able to use the variable also without self and what restrictions that brings with it?

推荐答案

self.property = value;

等于:

[self setProperty:value];

也就是说,使用self.的声明通过对象的访问器方法进行,而不是使用直接访问.

That is to say, the declaration using self. goes through the object's accessor method, rather than using direct access.

它们有不同的因果关系.也许最值得注意的是,如果不小心使用直接访问,通常会导致引用计数问题(泄漏/僵尸).访问器负责处理内存管理-是综合的,还是您自己实现的.

They have different causes and effects. Perhaps the most notable is that direct access will often leads to reference count issues (leaks/zombies) if not used with care. The accessor is responsible for handling memory management - if synthesised, or if you implement it yourself.

一般规则::您应该优先使用访问器(self.blah = thing;)而不是直接访问(blah = thing;),直到您知道何时以及为什么要对此规则进行例外.

The General Rule: You should favor using the accessors (self.blah = thing;) over direct access (blah = thing;) until you know when and why you would make exceptions to this rule.

立即例外:一般规则有一个例外:不要在部分构造状态下使用访问器,例如对象的初始化程序或dealloc.在这种情况下,请使用直接访问权限:

The immediate exception: There is one exception to the general rule: Do not use the accessors in partially constructed states, such as the object's initializer or dealloc. In those cases, use direct access:

- (id)init
{
  self = [super init];
  if (0 != self) {
    things = [NSArray new];
  }
  return self;
}

- (void)dealloc << not needed with ARC, in this case
{
  [things release], things = 0;
  [super dealloc];
}

更新

描述巴伐利亚人对该错误的怀疑:

Describing Bavarious' suspicion of the error:

听起来您已经声明了一个实例变量,但又没有声明 关联属性或适当的访问器(例如setter).这是带有ivars和属性的类声明的分解:

It sounds like you have declared an instance variable, but have not declared an associated property or proper accessors (e.g. the setter). Here's a breakdown of a class' declaration with ivars and properties:

@interface MONObject : NSObject
{
@private
  NSMutableArray * myMutableArray; << declares an instance variable
}

// the property declaration:
@property (nonatomic, retain, readwrite) NSMutableArray * myMutableArray;
// adds the accessors:
//   - (NSMutableArray *)myMutableArray; << getter
//   - (void)setMyMutableArray:(NSMutableArray *)arg; << setter
// to the class' interface.

@end


@implementation MONObject

// @synthesize below generates the accessors for the property
// myMutableArray, using "myMutableArray" ivar by default.
@synthesize myMutableArray;

- (void)zumBeispiel
{
  NSUInteger count = 0;

  // direct access:
  count = [myMutableArray count];
  // is equal to:
  count = [self->myMutableArray count];

  // Access via the getter:
  count = [self.myMutableArray count]; << equal to [self myMutableArray]
  // is equal to:
  count = [[self myMutableArray] count];
}

@end

这篇关于自变量和变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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