Objective-c:为 ABRecordRef 对象编写自定义 getter

发布于 2024-12-04 01:06:01 字数 998 浏览 0 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末的微笑 2024-12-11 01:06:01

如果这是未建模的属性,则不需要键值观察调用,例如 willAccessValueForKey:primitiveValueForKey:didAccessValueForKey:

过去,如果您对未建模的属性调用 primitiveValueForKey:,您将获得随机建模属性的值。我不确定这是否仍然成立,但这可能是这种情况下问题的根源,因为没有“原始”值可供访问,而只有值。

对于未建模的属性,只需使用传统的访问器,例如:

if (abRecordPerson == nil) {
  // load record by ID
  ABAddressBookRef abook = [Person getAddressBook];
  self.abRecordPerson = ABAddressBookGetPersonWithRecordID(abook, [self.abGlobalID intValue]);
 }
 return abRecordPerson;

If this is an unmodeled property, you don't need the key-value observing calls e.g willAccessValueForKey:, primitiveValueForKey: and didAccessValueForKey:.

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:

if (abRecordPerson == nil) {
  // load record by ID
  ABAddressBookRef abook = [Person getAddressBook];
  self.abRecordPerson = ABAddressBookGetPersonWithRecordID(abook, [self.abGlobalID intValue]);
 }
 return abRecordPerson;
失与倦" 2024-12-11 01:06:01

在您的@property声明中,我认为您需要使用分配或复制。也许它默认为 retain ,这会导致原始类型崩溃。

In your @property declaration, I think you need to use assign or copy. Maybe it is defaulting to retain which would cause a crash with primitive types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文