为什么这个属性需要'保留'? [英] Why does this property need the 'retain'?

查看:127
本文介绍了为什么这个属性需要'保留'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定具有保留属性的类的以下定义:

  @interface FeedEntry:NSObject< NSCoding> 
{
NSURL * url;
NSData * source;
}

@property(retain)NSURL * url;
@property(retain)NSData * source;
@end

@implementation FeedEntry

@synthesize url;
@synthesize source;

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:url forKey:@url];
[coder encodeObject:source forKey:@source];
}

为什么initWithCoder方法中的url属性需要retain p>

   - (id)initWithCoder:(NSCoder *)coder 
{
url = [[coder decodeObjectForKey:@ url] retain];
source = [coder decodeObjectForKey:@source];

NSLog(@got url =%@ \\\
,url);
return self;
}

具体来说,为什么合成的get url ? (我猜,source属性也需要一个retain)。

解决方案

>



设置时:

  url = [[coder decodeObjectForKey :@url] retain] 

您不是使用 @property 。您正在手动设置实例变量 url 的值。因此,您还必须手动保留的值。



要使用合成属性设置变量,而不是调用:

  [self setUrl:[coder decodeObjectForKey:@url]]; 

  self.url = [coder decodeObjectForKey:@url]; 

这两种形式都将使用合成的方法,并处理



Objective-C, @property @synthesize 关键字自动为您创建getter和setter方法:

  @interface MyClass 
{
id someValue;
}
@property(retain)id someValue;
@end

@implementation MyClass
@synthesize someValue;
@end

等效于:

  @interface MyClass 
{
id someValue;
}
- (id)someValue;
- (void)setSomeValue:(id)newValue;
@end

@implementation MyClass
- (id)someValue {return someValue; }
- (void)setSomeValue:(id)newValue
{
[newValue retain];
[someValue release];
someValue = newValue;
}
@end

成员变量和具有相同名称的属性。如果您按名称引用成员变量,那么您将绕过合成的属性方法。


Given the following definition of a class with retain properties:

@interface FeedEntry : NSObject<NSCoding>
{
    NSURL*  url;
    NSData* source;
}

@property (retain) NSURL*   url;
@property (retain) NSData*  source;
@end

@implementation FeedEntry

@synthesize url;
@synthesize source;

-(void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeObject:url     forKey:@"url"];
    [coder encodeObject:source  forKey:@"source"];
}

Why does the url property in initWithCoder method need the "retain":

-(id)initWithCoder:(NSCoder*)coder
{
    url = [[coder decodeObjectForKey:@"url"] retain];
    source  = [coder decodeObjectForKey:@"source"];

    NSLog(@"got url=%@\n", url);
    return self;
}

Specifically, why doesn't the synthesized "get url" method retain the object? (I'm guessing the source property will need a retain as well).

解决方案

Quick answer:

When you set:

url = [[coder decodeObjectForKey:@"url"] retain];

you are not using the @property. You are manually setting the value of the instance variable url. You must, therefore, also manually retain the value.

To set the variable using the synthesized properties, you would instead call:

[self setUrl:[coder decodeObjectForKey:@"url"]];

or

self.url = [coder decodeObjectForKey:@"url"];

Either of these forms would make use of the synthesized methods, and handle the retain automatically.

Details:

In Objective-C, the @property and @synthesize keywords automatically create the getter and setter methods for you:

@interface MyClass
{
    id someValue;
}
@property (retain) id someValue;
@end

@implementation MyClass
@synthesize someValue;
@end

Is equivalent to:

@interface MyClass
{
    id someValue;
}
- (id)someValue;
- (void)setSomeValue:(id)newValue;
@end

@implementation MyClass
- (id)someValue { return someValue; }
- (void)setSomeValue:(id)newValue
{
    [newValue retain];
    [someValue release];
    someValue = newValue;
}
@end

This creates an important distinction between the "internal" member variable and the property having the same name. If you reference the member variable by name, you are bypassing the synthesized property methods.

这篇关于为什么这个属性需要'保留'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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