如何“打破"核心数据的值何时发生变化? [英] How to "break" when a value of core data change?

查看:31
本文介绍了如何“打破"核心数据的值何时发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误,anImage.URL 的值突然变为 nil.anImage 是 Image 类的对象.一个数组中有 5 个这样的值.不知何故,其中至少有一个人的 URL 以某种方式变成了 nil.

I have this bug where the value of anImage.URL suddenly become nil. anImage is an object of class Image. There are 5 such values in an array. Somehow at least one of them have their URL turn into nil somehow.

我不知道那个东西在哪里改变了.

I have no idea where that thing is changed.

所以我将其设置为 anImage.URL = something 的类别

So I set this up as categories for anImage.URL = something

-(void) setURL:(NSString *)URL
{

    [super setValue:URL forKey:NSStringFromSelector(@selector(URL))];
}

并在那里设置断点.

然而,它成为一个问题,因为它只是递归地调用自己.

However, it becomes a problem because it simply recursively call itself.

我该怎么办?

当值更改不起作用时,调试器会中断.毕竟它是一个数组.无论如何我应该设置观看什么?

The debugger break when value change don't really work. After all it's an array. What should I set to watch anyway?

我不知道如何使用 keyValueObserving.

I am not sure how to use keyValueObserving.

基本上我想重新实现 -(void) setURL:(NSString *)URL 以完全像以前一样工作.唯一的区别是我想在那里放置一个断点.

Basically I want to reimplement -(void) setURL:(NSString *)URL to work exactly like before. The only difference is I want to put a breakpoint there.

推荐答案

您可以实现自定义的 setter 方法来调试或设置断点.

You can implement a custom setter method for debugging or setting a breakpoint.

一个例子可以在 核心数据编程指南"中的自定义属性和一对一关系访问器方法.

您在托管对象子类Image"上创建一个类别(例如MyAccessors")(如果您还没有).在实现文件Image+MyAccessors.m"中添加

You create a category (e.g. "MyAccessors") on the managed object subclass "Image" (if you don't have one already). In the implementation file "Image+MyAccessors.m" you add

@interface Image (PrimitiveAccessors)
- (void) setPrimitiveURL:(NSString *)newURL;
@end

@implementation Image (MyAccessors)

- (void)setURL:(NSString *)newURL
{
    [self willChangeValueForKey:@"URL"];
    [self setPrimitiveURL:newURL];
    [self didChangeValueForKey:@"URL"];
}

@end

技巧"是(我花了一些时间才弄明白),您必须在不同类别中声明原始访问器,而不是在您实现自定义的类别中访问器方法.

The "trick" is (and it took me some time to figure this out), that you have to declare the primitive accessor in a different category, not in the category where you implement the custom accessor methods.

或者,您可以像这样实现自定义访问器:

Alternatively, you can implement the custom accessor like this:

- (void)setURL:(NSString *)newURL
{
    [self willChangeValueForKey:@"URL"];
    [self setPrimitiveValue:newURL forKey:@"URL"];
    [self didChangeValueForKey:@"URL"];
}

在这种情况下,您不必声明原始访问器方法.

In this case you don't have to declare the primitive accessor method.

这篇关于如何“打破"核心数据的值何时发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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