Objective-c:为 ABRecordRef 对象编写自定义 getter
我正在尝试为 ABRecordRef 类型(C 类型)的属性编写自定义 getter。我不理解它的内存语义,并且每当访问该属性时我都会崩溃。这就是我正在做的事情:
@interface Person : NSManagedObject{
...
}
@property (nonatomic) ABRecordRef abRecordPerson;
@implementation Person
@synthesize abRecordPerson;
- (ABRecordRef) abRecordPerson
{
NSLog(@"start abRecordPerson");
ABRecordRef record = NULL;
[self willAccessValueForKey:@"abRecordPerson"];
record = [self primitiveValueForKey:@"abRecordPerson"];
[self didAccessValueForKey:@"abRecordPerson"];
if (record == NULL) {
// load record by ID
ABAddressBookRef abook = [Person getAddressBook];
record = ABAddressBookGetPersonWithRecordID(abook, [self.abGlobalID intValue]);
}
return record;
}
但是当我尝试使用 self.abRecordPerson 在同一 Person 类中的任何位置访问它时,我遇到了崩溃(EXC_BAD_ACCESS)。
有什么想法我可能做错了什么吗?我猜我在记忆中做错了什么。但是,如果我将 abRecordPerson 声明为动态属性并将其添加到 Person 实体的核心数据模型中,则此代码有效。由于无论如何我都无法真正保留该对象,因此我决定将其从模型中取出,将动态更改为合成,从那时起我就无法让它工作了。
谢谢。
I'm trying to write a custom getter for a property with type ABRecordRef (which is a C-type). I don't understand the memory semantics of this, and I keep getting a crash whenever the property is accessed. Here's what I'm doing:
@interface Person : NSManagedObject{
...
}
@property (nonatomic) ABRecordRef abRecordPerson;
@implementation Person
@synthesize abRecordPerson;
- (ABRecordRef) abRecordPerson
{
NSLog(@"start abRecordPerson");
ABRecordRef record = NULL;
[self willAccessValueForKey:@"abRecordPerson"];
record = [self primitiveValueForKey:@"abRecordPerson"];
[self didAccessValueForKey:@"abRecordPerson"];
if (record == NULL) {
// load record by ID
ABAddressBookRef abook = [Person getAddressBook];
record = ABAddressBookGetPersonWithRecordID(abook, [self.abGlobalID intValue]);
}
return record;
}
But when I try to access it anywhere in the same Person class with self.abRecordPerson, I get a crash (EXC_BAD_ACCESS).
Any ideas what I might be doing wrong? I'm guessing I'm doing something wrong in the memory. But this code worked if I declare the abRecordPerson as a dynamic property and added it to the Core Data model for the Person entity. Since I can't really persist the object anyways, I decided to take it out of the model, change dynamic to synthesize, and I can't get it to work from then on.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这是未建模的属性,则不需要键值观察调用,例如
willAccessValueForKey:
、primitiveValueForKey:
和didAccessValueForKey:
。过去,如果您对未建模的属性调用
primitiveValueForKey:
,您将获得随机建模属性的值。我不确定这是否仍然成立,但这可能是这种情况下问题的根源,因为没有“原始”值可供访问,而只有值。对于未建模的属性,只需使用传统的访问器,例如:
If this is an unmodeled property, you don't need the key-value observing calls e.g
willAccessValueForKey:
,primitiveValueForKey:
anddidAccessValueForKey:
.Used to, if you called
primitiveValueForKey:
on an unmodeled property, you would get the value of a random modeled property instead. I'm not sure if that still holds but it is likely the source of the problem in this case because there is no "primitive" value to access but just the value.For an unmodeled property just use a conventional accessor something like:
在您的@property声明中,我认为您需要使用分配或复制。也许它默认为
retain
,这会导致原始类型崩溃。In your
@property
declaration, I think you need to useassign
orcopy
. Maybe it is defaulting toretain
which would cause a crash with primitive types.