gpt4 book ai didi

iOS swift : init not being fired?

转载 作者:行者123 更新时间:2023-11-28 09:00:10 24 4
gpt4 key购买 nike

我有一个简单的类:

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)
}
}
}

(不要介意 btDiscoverySharedInstance,那是另一个类)

当我调用 var foo = BTManager() 时,我没有在控制台中看到“Init BTManager”。这是为什么?我不知道为什么这行不通...

最佳答案

我从一个结构中调用我的初始化器——显然 swift 不喜欢这样。

关于iOS swift : init not being fired?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32125284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com