Swift2 当需要自引用时,如何在Swift中初始化CBCentralManager

Swift2 当需要自引用时,如何在Swift中初始化CBCentralManager,swift2,initializer,Swift2,Initializer,初始化CBCentralManager实例的好方法是什么,它需要一个委托,并且通常指向所属类 我可以将该属性声明为隐式未包装的可选属性,但作为一般做法,这样做似乎不太迅速,也不太安全 或者,我可以将该属性声明为可选属性。但是由于CBCentralManager的初始值设定项没有声明为可失败,因此将实例声明为可失败似乎没有意义 隐式展开可选: class MyUnwrappedOptional: NSObject, CBCentralManagerDelegate { var centra

初始化CBCentralManager实例的好方法是什么,它需要一个委托,并且通常指向所属类

我可以将该属性声明为隐式未包装的可选属性,但作为一般做法,这样做似乎不太迅速,也不太安全

或者,我可以将该属性声明为可选属性。但是由于CBCentralManager的初始值设定项没有声明为可失败,因此将实例声明为可失败似乎没有意义

隐式展开可选:

class MyUnwrappedOptional: NSObject, CBCentralManagerDelegate {
    var centralManager: CBCentralManager! 

    func init() {
        super.init()

        centralManager = CBCentralManager(delegate: self, queue: nil, options:nil)

        // Subsequent usages of centralManager in other methods of this class don't require additional unwrapping.
        centralManager.scanForPeripheralsWithServices(services, options: nil)       
    }
}
class MyOptionalClass: NSObject, CBCentralManagerDelegate {
    var centralManager: CBCentralManager?

    func init() {
        super.init()

        centralManager = CBCentralManager(delegate: self, queue: nil, options:nil)

        // Subsequent usages of centralManager in other methods of this class require optional checks:
        if let central = centralManager {
            central.scanForPeripheralsWithServices(services, options: nil)      
        }

        // :: or ::
        central!.scanForPeripheralsWithServices(services, options: nil)
    }
}
使用可选选项:

class MyUnwrappedOptional: NSObject, CBCentralManagerDelegate {
    var centralManager: CBCentralManager! 

    func init() {
        super.init()

        centralManager = CBCentralManager(delegate: self, queue: nil, options:nil)

        // Subsequent usages of centralManager in other methods of this class don't require additional unwrapping.
        centralManager.scanForPeripheralsWithServices(services, options: nil)       
    }
}
class MyOptionalClass: NSObject, CBCentralManagerDelegate {
    var centralManager: CBCentralManager?

    func init() {
        super.init()

        centralManager = CBCentralManager(delegate: self, queue: nil, options:nil)

        // Subsequent usages of centralManager in other methods of this class require optional checks:
        if let central = centralManager {
            central.scanForPeripheralsWithServices(services, options: nil)      
        }

        // :: or ::
        central!.scanForPeripheralsWithServices(services, options: nil)
    }
}

在初始化每个没有默认值且不是可选(默认值为
nil
)的非
lazy
属性之前,没有办法在
init
方法中使用
self

如果您总是在
init
中初始化
centralManager
,并且没有可能使其
nil
的代码,我会说
CBCentralManager声明是一个不错的选择。这是隐式展开可选类型的主要用途之一

以下是摘录自:

有时,从程序的结构可以清楚地看出,一个可选的 始终有一个值,在第一次设置该值之后。在这些情况下 有助于消除检查和展开可选值的需要 每次访问它时,因为可以安全地假定它具有 永远珍惜

这些类型的选项定义为隐式展开 可选。通过放置 后面是感叹号(字符串!),而不是问号(字符串?) 要使其成为可选的类型

如果程序逻辑允许它在可能使用时的某个点为
nil
。那么一个普通的可选类型就是合适的选择

另一种可能的选择是将
centralManager
属性声明为。如果您这样做,则在您访问它之前不会创建它,但您将能够引用
self
,并将其设置为非可选。当您需要创建它时,将决定您是否使用此选项

lazy var centralManager: CBCentralManager = { [unowned self] () -> CBCentralManager in
    CBCentralManager.init(delegate: self, queue: nil, options: [:])
}()