gpt4 book ai didi

ios - 在 XCode 中使用 CocoaMQTT 时 iOS 应用程序出错

转载 作者:行者123 更新时间:2023-11-28 07:57:30 27 4
gpt4 key购买 nike

我想制作一个简单的应用程序来使用 MQTT 切换 LED,所以我使用 CocoaMQTT。当我调用 mqtt.publish 时,应用程序崩溃了。我是 iOS 开发的新手,所以我对此了解不多。

向我显示的错误是:

 2017-12-04 01:18:27.828064-0200 Ex_Oficina_MQTT[33361:3493271] +[NSTimer after::]: unrecognized selector sent to class 0x11479cd60
2017-12-04 01:18:27.836738-0200 Ex_Oficina_MQTT[33361:3493271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSTimer after::]: unrecognized selector sent to class 0x11479cd60'
*** First throw call stack:
(
0 CoreFoundation 0x00000001144d71ab __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000110638f41 objc_exception_throw + 48
2 CoreFoundation 0x0000000114557974 +[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000011445a0a8 ___forwarding___ + 1432
4 CoreFoundation 0x0000000114459a88 _CF_forwarding_prep_0 + 120
5 CocoaMQTT 0x000000010ffbd6ec _T09CocoaMQTT0A15MQTTFrameBufferC12tryTransportyyF + 620
6 CocoaMQTT 0x000000010ffc1f28 _T09CocoaMQTT0A15MQTTFrameBufferC3addSbAA0aC7PublishCFTf4gn_n + 680
7 CocoaMQTT 0x000000010ffb2889 _T09CocoaMQTTAAC7publishs6UInt16VAA0A11MQTTMessageCFTf4gn_n + 713
8 CocoaMQTT 0x000000010ffadfad _T09CocoaMQTTAAC7publishs6UInt16VSS_SS10withStringAA0A7MQTTQOSO3qosSb8retainedSb3duptF + 189
9 Ex_Oficina_MQTT 0x000000010fc251ed _T015Ex_Oficina_MQTT14ViewControllerC5clickySo8UIButtonC6sender_tF + 1453
10 Ex_Oficina_MQTT 0x000000010fc2544c _T015Ex_Oficina_MQTT14ViewControllerC5clickySo8UIButtonC6sender_tFTo + 60
11 UIKit 0x0000000110ed7275 -[UIApplication sendAction:to:from:forEvent:] + 83
12 UIKit 0x00000001110544a2 -[UIControl sendAction:to:forEvent:] + 67
13 UIKit 0x00000001110547bf -[UIControl _sendActionsForEvents:withEvent:] + 450
14 UIKit 0x00000001110536ec -[UIControl touchesEnded:withEvent:] + 618
15 UIKit 0x0000000110f4cbbb -[UIWindow _sendTouchesForEvent:] + 2807
16 UIKit 0x0000000110f4e2de -[UIWindow sendEvent:] + 4124
17 UIKit 0x0000000110ef1e36 -[UIApplication sendEvent:] + 352
18 UIKit 0x0000000111834434 __dispatchPreprocessedEventFromEventQueue + 2809
19 UIKit 0x0000000111837089 __handleEventQueueInternal + 5957
20 CoreFoundation 0x000000011447a231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
21 CoreFoundation 0x0000000114519e41 __CFRunLoopDoSource0 + 81
22 CoreFoundation 0x000000011445eb49 __CFRunLoopDoSources0 + 185
23 CoreFoundation 0x000000011445e12f __CFRunLoopRun + 1279
24 CoreFoundation 0x000000011445d9b9 CFRunLoopRunSpecific + 409
25 GraphicsServices 0x0000000116b279c6 GSEventRunModal + 62
26 UIKit 0x0000000110ed55e8 UIApplicationMain + 159
27 Ex_Oficina_MQTT 0x000000010fc267c7 main + 55
28 libdyld.dylib 0x000000011545ad81 start + 1
29 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

我的代码是:

class ViewController: UIViewController {


@IBOutlet var btToggle: UIButton!
override func viewDidLoad() {
super.viewDidLoad()

btToggle.addTarget(self,action:#selector(self.click), for: UIControlEvents.touchUpInside )


// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16){
print("Mensagem publicada")
}

@objc func click(sender: UIButton) {

let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID,host:"m12.cloudmqtt.com", port: 17357)

mqtt.username = ""
mqtt.password = ""
mqtt.willMessage = CocoaMQTTWill(topic: "/led", message: "ligar")
mqtt.keepAlive = 60
mqtt.delegate = self as? CocoaMQTTDelegate
mqtt.connect()
mqtt.willMessage = CocoaMQTTWill(topic: "/led", message: "ligar")

mqtt.publish("/led", withString: "ligar")

print("click")
}

我一直在创建新的 mqtt,因为我无法在全局创建。

最佳答案

很可能发生的情况是您的 mqtt.publish 调用在 mqtt.connect() 完成之前发生。 connect() 是一个异步调用,需要在调用订阅之前完成。

在您的委托(delegate)方法中,您可以在确认服务已连接后调用您的publish

func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
debugPrint("did Connect Ack: \(ack)")
mqtt.publish("/led", withString: "ligar")
}

关于ios - 在 XCode 中使用 CocoaMQTT 时 iOS 应用程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47626088/

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