iPhone实例变量问题 [英] iPhone Instance Variable Issue

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

问题描述

我想弄清楚将myString合成到实例变量_myString的目的是什么。

I am trying to figure out what is the purpose of synthesizing myString to the instance variable _myString.

这样做的目的是什么?我倾向于注意到很多人这样做。

What is the purpose of doing this? I tend to notice a lot of people doing this.

此外,我应该释放实例变量并将实例变量设置为nil,这是正确的。

Also I should release the instance variable and set the instance variable to nil, is that correct.

ViewController.h

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSString *_myString;
}

@property (nonatomic, retain) NSString *myString;

@end

ViewController.m

ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize myString = _myString;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.myString = [[NSString alloc] initWithFormat:@"Hello"];

    _myString = [[NSString alloc] initWithFormat:@"Goodbye"];

    NSLog(@"%@\t%@", self.myString, _myString);
}

- (void)viewDidUnload
{
    _myString = nil;
    [super viewDidUnload];
}

- (void)dealloc
{
    [_myString release];
    [super dealloc];
}

@end


推荐答案

当你在目标c中@synthesize一个对象类型属性时,编译器将为该实例变量生成两个隐藏方法。因此,当您引用 myObject.myString 时,不仅会返回_myString的指针,还会生成 - (NSString *)myString 方法。类似地,如果你为属性赋值: myObject.myString = @foo; 它将被编译为 [myObject setMyString:@foo ];

When you @synthesize an object type property in objective c, the compiler will generate two hidden methods for that instance variable. Thus when you refer myObject.myString, not only the pointer of _myString will be returned, but the generated - (NSString*) myString method will be called. Similarly if you assign something to the property: myObject.myString = @"foo"; it will be compiled to [myObject setMyString:@"foo"];.

中的内容 - (NSString *)myString - (void)setMyString:(NSString *)theString 取决于您在声明属性时指定的关键字。最常见的是 assign 这个简单分配你给出的指针:

What is in - (NSString*) myString and - (void) setMyString:(NSString*) theString depends on the keywords you specify when declaring the property. The most common is assign that simple assigns the pointer you give:

- (void) setMyString:(NSString*) theString 
{
     _myString = theString;
}

- (NSString*) myString
{
    return _myString;
}

这与声明_myString是一个公共变量并没有多大区别,但它是更明确的方式来说这个变量可以直接从外部访问。

This makes not too much difference from declaring _myString a public variable, but it is more explicit way to say that this variable can be directly accessed from outside.

反过来, retain 生成类似的setter方法(getter方法将是相同的):

In turn, retain generates similar setter method (getter method will be the same):

- (void) setMyString:(NSString*) theString 
{
     [theString retain];
     [_myString release];
     _myString = theString;
}

你可以看到它负责你传递的对象的内存管理进入财产。换句话说,您可以确定在您拥有该对象之前不会释放该对象,因此当您取得其所有权时,您无需手动保留 。这使得编写管理内存而没有泄漏的代码变得更加方便。请注意,在dealloc中,您仍然必须将nil应用于您的属性才能释放它存储的最后一个对象。

You can see that this takes care of the memory management of the object you passed into the property. In other words you can be sure that the object will be not released until you have it in your property, thus you don't have to retain it manually when you take its ownership. This makes much more convenient to write code that manages the memory without leaks. Note, in the dealloc you still have to apply nil to your property in order to release the last object it stored.

当他们的数据不是来自时,另一类属性实例变量,但来自其他一些数据源,如数据库。例如,核心数据的托管对象的自动生成属性可以正常工作。

Another category of properties when their data does not come from instance variables but from some other data source like a database. The auto-generated properties of the Managed Objects of Core Data for example work so.

最后,您还可以定义自己的getter和setter。例如,围绕一些经常使用的 NSUserDefaults 设置写一个属性包装器,这将有助于其访问:

At last, you can also define your own getters and setters. A good idea for example to write a property wrapper around some frequently used NSUserDefaults settings, that will facilitate its access:

@interface Bar 

@property (nonatomic, assign) NSString* foo;

@end

@implementation Bar

- (void) setFoo:(NSString *)theFoo
{
    [[NSUserDefaults standardUserDefaults] setObject:theFoo 
                                              forKey:@"settings.foo"];
}

- (NSString*) foo
{
    return [[NSUserDefaults standardUserDefaults] 
        stringForKey:@"settings.foo"];
}

@end

myBar.foo = @"foobar"; // automatically persisted between application runs

另请阅读以下内容:高级内存管理目标C中声明的属性

Read these also: Advanced memory management and Declared properties in objective C

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

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