gpt4 book ai didi

objective-c - NSManagedObjectContextObjectsDidChangeNotification userInfo 字典

转载 作者:行者123 更新时间:2023-12-03 16:51:59 24 4
gpt4 key购买 nike

我正在我的应用程序中使用 NSManagedObjectContextObjectsDidChangeNotification 通知,我现在已经知道如何使用它了。因为我使用了下面的代码来添加观察者......

- (void) awakeFromNib {
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self
selector:@selector(syncKVO:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:nil];
}

- (void)syncKVO:(id)sender {
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
name:NSManagedObjectContextObjectsDidChangeNotification
object:nil];

// Do stuff.

[nc addObserver:self
selector:@selector(syncKVO:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:nil];

}

但是我想检查 userInfo 字典以确保该方法确实必须被触发,我该怎么做?

最佳答案

查看documentation for NSManagedObject 给你一个答案。

通知有 three instance methods其中之一是 -userInfo 方法,它返回 userInfo 字典

看起来你的syncKVO:方法不正确;通知处理程序应将通知对象作为参数。

documentation对于您正在查找的通知,显示此通知的字典中的键,您可以使用类似的内容来获取您可能需要的内容:

- (void)syncKVO:(NSNotification *)notification {
NSDictionary *userInfoDictionary = [notification userInfo];
NSSet *deletedObjects = [userInfoDictionary objectForKey:NSDeletedObjectsKey];

// do what you want with the deleted objects
}

关于objective-c - NSManagedObjectContextObjectsDidChangeNotification userInfo 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1660280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com