简体   繁体   中英

iOS Swift: init not being fired?

I have a simple class:

import UIKit

class BTManager: NSObject{

    var allowTX = false

    override init(){
        super.init()
        print("Init BTManager")
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("connectionChanged:"), name: BLEServiceChangedStatusNotification, object: nil)
        btDiscoverySharedInstance
    }

    func connectionChanged(notification: NSNotification) {
        // Connection status changed. Indicate on GUI.
        let userInfo = notification.userInfo as! [String: Bool]

        dispatch_async(dispatch_get_main_queue(), {
            // Set image based on connection status
            if let isConnected: Bool = userInfo["isConnected"] {
                if isConnected {
                    self.allowTX = true
                    variables.connected = true
                } else {
                    self.allowTX = false
                    variables.connected = false
                }
            }
        });
    }

    func sendSerial(serial: String) {
        // 1
        if !allowTX {
            return
        }

        if let bleService = btDiscoverySharedInstance.bleService {
            bleService.sendSerial(serial)
        }
    }
}

(Don't mind the btDiscoverySharedInstance, that's another class)

When I call var foo = BTManager() I do not see "Init BTManager" in the console. Why is that? I have no idea why this wouldn't work...

我从结构调用初始化器-显然swift不喜欢那样。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM