Objective c 如何在目标C中创建事件循环?

Objective c 如何在目标C中创建事件循环?,objective-c,macos,bluetooth,event-loop,Objective C,Macos,Bluetooth,Event Loop,我正在尝试使用corebooth模块列出命令行OSX应用程序中检测到的所有蓝牙设备 到目前为止,我所拥有的是这样的: @import CoreBluetooth; @interface MyCentralManager : NSObject<CBCentralManagerDelegate> - (void) centralManagerDidUpdateState: (CBCentralManager *) central; - (void) centralManager:(CB

我正在尝试使用
corebooth
模块列出命令行OSX应用程序中检测到的所有蓝牙设备

到目前为止,我所拥有的是这样的:

@import CoreBluetooth;

@interface MyCentralManager : NSObject<CBCentralManagerDelegate>
- (void) centralManagerDidUpdateState: (CBCentralManager *) central;
- (void) centralManager:(CBCentralManager *) central
    didDiscoverPeripheral:(CBPeripheral *) peripheral
        advertisementData:(NSDictionary *) advertisementData
                     RSSI:(NSNumber *)RSSI;
@end

@implementation MyCentralManager
- (void) centralManagerDidUpdateState: (CBCentralManager *) central
{
    NSLog(@"State changed...");
}

- (void) centralManager:(CBCentralManager *) central
    didDiscoverPeripheral:(CBPeripheral *) peripheral
        advertisementData:(NSDictionary *) advertisementData
                     RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discovered %@", peripheral.name);
}
@end

int main() {
    MyCentralManager* myCentralManager = [[MyCentralManager alloc] init];
    CBCentralManager* cbCentralManager = [[CBCentralManager alloc] initWithDelegate:myCentralManager queue:nil options:nil];

    NSLog(@"Scanning devices now !");

    [cbCentralManager scanForPeripheralsWithServices:nil options:nil];
    sleep(5); // Wait 5 seconds before stopping the scan.
    [cbCentralManager stopScan];

    NSLog(@"Scanning devices ended.");

    return 0;
}
@import-corebooth;
@接口MyCentralManager:NSObject
-(无效)中央管理者数据状态:(CBCentralManager*)中央;
-(无效)中央经理:(CBCentralManager*)中央
DidDiscoveryPeripheral:(CBPeripheral*)peripheral
advertisementData:(NSDictionary*)advertisementData
RSSI:(NSNumber*)RSSI;
@结束
@实现MyCentralManager
-(void)CentralManagerDipDateState:(CBCentralManager*)central
{
NSLog(@“状态已更改…”);
}
-(无效)中央经理:(CBCentralManager*)中央
DidDiscoveryPeripheral:(CBPeripheral*)peripheral
advertisementData:(NSDictionary*)advertisementData
RSSI:(NSNumber*)RSSI
{
NSLog(@“Discovered%@”,peripheral.name);
}
@结束
int main(){
MyCentralManager*MyCentralManager=[[MyCentralManager alloc]init];
CBCentralManager*CBCentralManager=[[CBCentralManager alloc]initWithDelegate:myCentralManager队列:nil选项:nil];
NSLog(@“立即扫描设备!”);
[cbCentralManager扫描外围设备服务:无选项:无];
sleep(5);//停止扫描前等待5秒钟。
[cbCentralManager停止扫描];
NSLog(@“扫描设备结束”);
返回0;
}
现在这根本不起作用,因为我从未得到任何
“状态更改…”
“发现…”
日志输出

我以前从未真正编写过任何Objective C应用程序,所以我可能忽略了显而易见的一点。如果让我猜我做错了什么,我会假设:

  • 我从来没有真正进入状态更改委托方法,因此我假设我的第一个错误是:
    sleep()
    'ing,我必须运行某种类型的事件循环,以便底层系统有机会通知我状态更改。
我基本上被困在这一点上:我没有GUI,也不想要GUI,但无法找到运行事件循环的方法(假设缺少GUI)。我该怎么做


正如我所说的,这实际上是我第一次尝试使用Objective C,所以不要害怕说出显而易见的情况。

只需运行线程的运行循环即可

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
更多信息


顺便说一句:您不必声明协议中已经声明的方法。

只需运行线程的运行循环即可

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
更多信息


顺便说一句:您不必声明协议中已经声明的方法。

只需运行线程的运行循环即可

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
更多信息


顺便说一句:您不必声明协议中已经声明的方法。

只需运行线程的运行循环即可

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
更多信息



顺便说一句:您不必声明已在协议中声明的方法。

您的MyCentralManager实例可能未被保留,因为委托属性CBCentralManager被声明为弱。如果您在manager类中重写dealloc,是否在扫描完成之前调用它?@PatrickGoley会对此有任何链接/引用吗?我不确定是什么原因导致我的代码中的属性变弱。不是您的代码导致它变弱,如果查看CBCentralManager的标头,您会看到它的委托属性被声明为弱,这是指向您的管理器的唯一引用instance@PatrickGoley好的,我明白了。谢谢你的解释!由于委托属性CBCentralManager被声明为弱,因此可能没有保留MyCentralManager实例。如果您在manager类中重写dealloc,是否在扫描完成之前调用它?@PatrickGoley会对此有任何链接/引用吗?我不确定是什么原因导致我的代码中的属性变弱。不是您的代码导致它变弱,如果查看CBCentralManager的标头,您会看到它的委托属性被声明为弱,这是指向您的管理器的唯一引用instance@PatrickGoley好的,我明白了。谢谢你的解释!由于委托属性CBCentralManager被声明为弱,因此可能没有保留MyCentralManager实例。如果您在manager类中重写dealloc,是否在扫描完成之前调用它?@PatrickGoley会对此有任何链接/引用吗?我不确定是什么原因导致我的代码中的属性变弱。不是您的代码导致它变弱,如果查看CBCentralManager的标头,您会看到它的委托属性被声明为弱,这是指向您的管理器的唯一引用instance@PatrickGoley好的,我明白了。谢谢你的解释!由于委托属性CBCentralManager被声明为弱,因此可能没有保留MyCentralManager实例。如果您在manager类中重写dealloc,是否在扫描完成之前调用它?@PatrickGoley会对此有任何链接/引用吗?我不确定是什么原因导致我的代码中的属性变弱。不是您的代码导致它变弱,如果查看CBCentralManager的标头,您会看到它的委托属性被声明为弱,这是指向您的管理器的唯一引用instance@PatrickGoley好的,我明白了。谢谢你的解释!谢谢你的回答,我还没有机会测试一下,但很快就会。投票人,你能解释一下你的投票吗?为什么你认为这是错误的?一个白痴:-2 1小时前的下一票-2 1小时前的下一票目标-C:在NSException原因中添加NSString-2 1小时前的下一票如何验证金额文本值-2 1小时前的下一票NSObject类初始化-2 1小时前的下一票类别与iOS中的实用程序类-2 1小时前的下一票整数NSPoupButton选定项的值-2 1小时前目标c中的下一票TDD:属性/常量