0

我正在使用自动合成的@property. 当我在方法中访问该属性时,我遇到了一个EXC_BAD_ACCESS异常。以下是我的实现文件中的相关代码部分:

#import "BBBluetoothController.h"
#import <IOBluetooth/IOBluetooth.h>

@interface BBBluetoothController ()

@property (nonatomic, strong) CBCentralManager *bluetoothManager;

@end

@implementation BBBluetoothController

- (instancetype)init {
    if (self = [super init]) {
        _bluetoothManager = [[CBCentralManager alloc] init];
        _bluetoothManager.delegate = self;
    }

    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        _bluetoothManager = [[CBCentralManager alloc] init];
        _bluetoothManager.delegate = self;
    }

    return self;
}

- (IBAction)startScanning:(id)sender {
    if (self.bluetoothManager.state == CBCentralManagerStatePoweredOn) {
        [self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
        self.isScanning = YES;
    }
}

@end

-startScanning:在方法的第一行抛出异常。深入堆栈跟踪,我看到异常是从CBCentralManager' 的实现内部抛出的:

0x7fff96c6ed49: leaq -413178944(%rip), %rax ; CBCentralManager._delegate

如果我闯入,-startScanning:我可以看到以下内容lldb

(lldb) po self
<BBBluetoothController: 0x1022213b0>
(lldb) po self.bluetoothManager
<CBConcreteCentralManager: 0x102222180>
(lldb) p self.bluetoothManager.state
(CBCentralManagerState) $2 = CBCentralManagerStateUnknown
(lldb) po self.bluetoothManager.delegate
<BBBluetoothController: 0x1022213b0>

此外,如果我引用实例变量-startScanning:而不是属性,那么一切都会顺利进行。我在这里遗漏了一些明显的东西吗?

编辑:

对于它的价值,这在 OS X 10.9 上运行良好,但在 OS X 10.8.5 上运行良好。

4

1 回答 1

0

哎呀!没有使用指定的初始化程序:-[CBCentralManager initWithDelegate:queue:]

于 2014-01-19T20:35:02.267 回答