- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有什么方法可以确保类发布特定的 NSNotification?
(我有一组类,我想在编译时(如果可能的话)强制该类发布所需的 NSNotification)。
或者,如果不可能,是否有任何解决方法?
最佳答案
在编译时预测运行时会发生什么基本上是不可能的。您可以获得的最接近的是静态分析,但即使这样也无法预测您自己的代码之外发生的任何事情,例如 Foundation 内部。
但是,您可以通过单元测试来执行此操作,因为测试运行程序实际上运行测试下的代码。
如果您还没有创建测试包目标,则需要创建它。您的目标将使用 SenTestingKit 来运行您创建的测试。 (在 iPhone 上,您还需要适用于 Mac 的 Google 工具箱。它们有 a handy tutorial on using GTM for iPhone tests 。)
您将创建一个 SenTestCase 子类来测试您的真实对象是否发布通知。它看起来像这样:
@interface FrobnitzerNotificationsTest: SenTestCase
{
BOOL frobnitzerDidCalibrate;
}
- (void) frobnitzerDidCalibrate:(NSNotification *)notification;
@end
@implementation FrobnitzerNotificationsTest
- (void) testFrobnitzerCalibratePostsNotification {
Frobnitzer *frobnitzer = …;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(frobnitzerDidCalibrate:)
name:FrobnitzerDidCalibrate
object:frobnitzer];
frobnitzerDidCalibrate = NO;
//This should post a notification named FrobnitzerDidCalibrate with the receiver as the object.
[frobnitzer calibrate];
//If it did, our notification handler set frobnitzerDidCalibrate to YES (see below).
[nc removeObserver:self
name:FrobnitzerDidCalibrate
object:frobnitzer];
STAssertTrue(frobnitzerDidCalibrate, @"Frobnitzer did not post a notification when we told it to calibrate");
}
- (void) frobnitzerDidCalibrate:(NSNotification *)notification {
frobnitzerDidCalibrate = YES;
}
@end
对于要测试的每个通知,您都需要一个实例变量和一种通知处理程序方法,并且对于要测试通知的每个方法都需要一个测试方法。
此外,如果使用 GTM,则必须用 GTMSenTestCase 替换上面的 SenTestCase。
关于objective-c - 强制某个类发布特定的 NSNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1062191/
我正在为我的 iPhone 应用开发一个类,我希望它能够注册并了解应用状态的变化(UIApplicationDidEnterBackgroundNotification 等)。有没有一种方法可以注册通
这是我得到的错误 Thread 1:EXC_BAD_ACCESS (code=2, address=0xb7ffffc) 在这条线上 [[NSNotificationCenter defaultCen
你能解释一下什么是NSNotification 的目的,以及我可以在哪些情况下使用是吗? 通知是否调用所有类在应用程序中,或者它是否调用特定的类,通过传递代表? 是否可以创建1通知,并在多个类(cla
我正在开发一个电子书阅读器,我遇到了以下问题。我正在使用 IBAction 方法来发布 NSNotification,一旦点击按钮,它就会调用操作方法。它第一次工作得非常好...每次我点击按钮时都必须
谁能解释一下 NSNotificationCenter 的重要性吗? 在哪里使用它们? NSNotificationCenter 与 AppDelegate 有什么区别? 最佳答案 Apple 在 C
在我的 NSApp 委托(delegate)中,我添加了一个对象的观察者,该对象是 NSWindow 子类,该对象在委托(delegate)本身中启动,并在单击窗口后发布通知。选择器也在委托(dele
我正在尝试在我的多线程应用程序中获取通知 NSTaskDidTerminateNotification,但无法使其正常工作。当我在单线程应用程序上测试它时,它似乎确实有效。在 init 中,我有 [[
在单线程中使用 NSNotifications 时是否存在竞争条件问题?这是一个示例方法: - (void) playerToggled: (NSNotification *) notificatio
WebKit 的 WebHistory API 按日期分隔其项目。因此,当日期发生变化时,我需要重新分配任何“昨天”和/或“(早些时候)今天”(或“明天”!)标签。有 NSNotification 吗
我计划编写一个小守护程序来检测另一个应用程序是否崩溃,一直认为系统会发送 NSWorkspaceDidTerminateApplicationNotification,但事实并非如此。 假设我不想创建
我正在阅读 iOS Big Nerd Ranch 书,其中一个示例显示了如何将观察者添加到 NSNotificaionCenter : [[NSNotificationCenter defau
我想要一种方法,可以暂停执行,直到收到通知后再继续。收到通知后它将继续执行。执行此操作的最佳方法是什么? 最佳答案 你可以使用 NSRunLoop - (BOOL)method { __block B
在我的游戏中,我正在发送 NSNotification 以在游戏过程中隐藏横幅广告,并在主菜单和游戏结束场景上显示横幅广告。这工作正常,除了由于某种原因每次我点击屏幕时都会调用隐藏广告的通知并且广告消
我的 didFinishLaunchingWithOptions 中有以下代码 NotificationCenter.default.addObserver( self,
什么相当于 Android 中的 NSNotification? 我需要能够发送和接收来自任意类(不一定是 Activity )的通知。 最佳答案 这种模式称为 EventBus,并且有一些用于此的库
我正在做一个关于 NSNotification 的简单教程,但在正确执行它时遇到了一些问题。当我单击第一个 View 上的按钮时,第二个 View 中的文本字段没有得到更新。当我通过在 receive
我有一个多线程程序通过 NSNotificationCenter 发送消息(addObserver:.. 和 postNotification:... 方法)。 线程订阅不同的通知,其中一些是共享的,
我在旋转设备时收到多个 UIKeyboardDidShowNotification 和 UIKeyboardDidHideNotification 通知,我不明白为什么。 我有一个应用程序,可以让您为
我经常在 viewDidLoad 中注册 NSNotification 并在 dealloc 中取消注册。我们在 ios 5 中没有 dealloc。我在哪里注销 NSNotification? 最佳
有没有办法在不延迟任何当前完成的代码的情况下触发 NSNotification(可能会触发大量工作)? 具体来说,我有一个 UIPanGestureRecognizer,它允许用户平移几个月、一月、二
我是一名优秀的程序员,十分优秀!