给定具有retain属性的类的以下定义:
@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"];
}
Run Code Online (Sandbox Code Playgroud)
为什么initWithCoder方法中的url属性需要"retain":
-(id)initWithCoder:(NSCoder*)coder
{
url = [[coder decodeObjectForKey:@"url"] retain];
source = [coder decodeObjectForKey:@"source"];
NSLog(@"got url=%@\n", url);
return self;
}
Run Code Online (Sandbox Code Playgroud)
具体来说,为什么合成的"获取url"方法不能保留对象?(我猜测源属性也需要保留).
e.J*_*mes 16
快速回答:
当你设置:
url = [[coder decodeObjectForKey:@"url"] retain];
Run Code Online (Sandbox Code Playgroud)
你没有使用@property.您是手动设置实例变量的值url.因此,您还必须手动设置retain该值.
要使用合成属性设置变量,您可以调用:
[self setUrl:[coder decodeObjectForKey:@"url"]];
Run Code Online (Sandbox Code Playgroud)
要么
self.url = [coder decodeObjectForKey:@"url"];
Run Code Online (Sandbox Code Playgroud)
这些形式中的任何一种都将使用合成的方法,并retain自动处理.
细节:
在Objective-C中,@property和@synthesize关键字自动为您创建getter和setter方法:
@interface MyClass
{
id someValue;
}
@property (retain) id someValue;
@end
@implementation MyClass
@synthesize someValue;
@end
Run Code Online (Sandbox Code Playgroud)
相当于:
@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
Run Code Online (Sandbox Code Playgroud)
这在"内部"成员变量和具有相同名称的属性之间创建了一个重要的区别.如果按名称引用成员变量,则绕过合成属性方法.
| 归档时间: |
|
| 查看次数: |
1548 次 |
| 最近记录: |