- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当我的类初始化时,它会将自己添加为一堆不同 Wi-Fi 通知的观察者。出于某种原因,当这些事情发生时,选择器不会运行。有任何想法吗?提前谢谢你。
-(id) init
{
if (self)
{
sself = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWModeDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWSSIDDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWBSSIDDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWCountryCodeDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWLinkDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWPowerDidChangeNotification object:nil];
更新:这是 handleNotification 方法:
-(void) handleNotification:(NSNotification*) notification
{
NSLog(@"Notification Received");
}
我已将 CoreWLAN 框架包含到我的项目中:
我已经下载了 CoreWLANWirelessManager.app,这是我用来引用的。奇怪的是,Apple 的代码使用了已弃用的通知,但它仍然有效。我尝试使用新的 API 和已弃用的 API,但均未成功。我不确定我是否可以在这里发布他们的代码,但实际上没有区别。选择器甚至具有相同的名称。
请不要犹豫,要求进一步详细说明。
更新(在 Dustin 的回答之后):我创建了一个新项目,希望能找出问题所在。我按照您的描述设置了 .h 和 .m 文件。可悲的是,我仍然没有收到任何通知。为了向您展示我没有撒谎(或发疯),我提供了在同一运行时截取的两个(相当拥挤的)屏幕截图。注意:(1. 我在 handleNotification: 方法中有一个断点。应用程序永远不会暂停。(2. 我添加了网络窗口以显示我的 Mac 在此运行时确实更改了 Wi-Fi 网络。(3. 没有 NSLoged
网络 1:
网络 2:
2012 年 5 月 17 日更新:Dustin 的回答是正确的,但 Wi-Fi 接口(interface)名称因应用程序运行的硬件而异。在我的例子中,(MacBook Air;没有以太网),我的 Wi-Fi 是 en0 而不是 en1。我设法从我妈妈的 iMac 上获取系统配置 plst 文件,Wi-Fi 称为 en1。以太网是 en0。谢谢大家的帮助。
最佳答案
为了获得这些通知,您需要持有一个 CWInterface 实例。你的 .h 看起来像这样
#import <Cocoa/Cocoa.h>
@class CWInterface;
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (retain) CWInterface *wirelessInterface;
@end
然后在你的 .m 文件中看起来像这样
#import "AppDelegate.h"
#import <CoreWLAN/CoreWLAN.h>
@implementation AppDelegate
@synthesize window = _window;
@synthesize wirelessInterface;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWModeDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWSSIDDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWBSSIDDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWCountryCodeDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWLinkDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWPowerDidChangeNotification object:nil];
self.wirelessInterface = [CWInterface interfaceWithName:@"en1"];
}
-(void) handleNotification:(NSNotification*) notification
{
NSLog(@"Notification Received");
}
@end
注意 CWInterface 属性,这是重要的一点
关于objective-c - NSNotification 的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10525858/
我遵循了一本名为“Sitepoint Full Stack Javascript with MEAN”的书中的教程,我刚刚完成了第 6 章,应该已经创建了一个带有“数据库”的“服务器”。数据库只不过是
在 Jquery 中,我创建两个数组,一个嵌入另一个数组,就像这样...... arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arra
这个问题在这里已经有了答案: What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wa
我被放在别人的代码上,有一个类用作其他组件的基础。当我尝试 ng serve --aot(或 build --prod)时,我得到以下信息。 @Component({ ...,
我正在测试一些代码,并使用数据创建了一个 json 文件。 问题是我在警报中收到“[object Object],[object Object]”。没有数据。 我做错了什么? 这是代码:
我想打印 [object Object],[object Object] 以明智地 "[[{ 'x': '1', 'y': '0' }, { 'x': '2', 'y': '1' }]]"; 在 ja
我有一个功能 View ,我正在尝试以特殊格式的方式输出。但我无法让列表功能正常工作。 我得到的唯一返回是[object Object][object Object] [object Object]
在使用优秀的 Sim.js 和 Three.js 库处理 WebGL 项目时,我偶然发现了下一个问题: 一路走来,它使用了 THREE.Ray 的下一个构造函数: var ray = new THRE
我正在使用 Material UI 进行多重选择。这是我的代码。 {listStates.map(col => (
我的代码使用ajax: $("#keyword").keyup(function() { var keyword = $("#keyword").val(); if (keyword.
我遇到了下一个错误,无法理解如何解决它。 Can't resolve all parameters for AuthenticationService: ([object Object], ?, [o
我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。 这是组件的代码: import { Component, OnInit } from '@angul
我正在开发 NodeJs 应用程序,它是博客应用程序。我使用了快速验证器,我尝试在 UI 端使用快速闪存消息将帖子保存在数据库中之前使用闪存消息验证数据,我成功地将数据保存在数据库中,但在提交表单后消
我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数
我继承了一个非常草率的项目,我的任务是解释为什么它不好。我注意到他们在整个代码中都进行了这样的比较 (IQueryable).FirstOrDefault(x => x.Facility == fac
我只是在删除数组中的对象时偶然发现了这一点。 代码如下: friends = []; friends.push( { a: 'Nexus', b: 'Muffi
这两个代码片段有什么区别: object = nil; [object release] 对比 [object release]; object = nil; 哪个是最佳实践? 最佳答案 object
我应该为其他人将从中继承的第一个父对象传递哪个参数,哪个参数更有效 Object.create(Object.prototype) Object.create(Object) Object.creat
我在不同的对象上安排不同的选择器 [self performSelector:@selector(doSmth) withObject:objectA afterDelay:1]; [self per
NSLog(@"%p", &object); 和 NSLog(@"%p", object); 有什么区别? 两者似乎都打印出一个内存地址,但我不确定哪个是对象的实际内存地址。 最佳答案 这就是我喜欢的
我是一名优秀的程序员,十分优秀!