简体   繁体   English

在XCode中使用CocoaMQTT时在iOS App上出现错误

[英]Error on iOS App when use CocoaMQTT in XCode

I want to make a simple app to toggle a led with MQTT, so I use the CocoaMQTT. 我想制作一个简单的应用程序来切换带MQTT的LED,因此我使用了CocoaMQTT。 When I call the mqtt.publish the app is crashed. 当我致电mqtt.publish时,应用程序崩溃了。 I'am newest to iOS develop, so I don't understant more things about this. 我是iOS开发的最新成员,因此我对此没有更多的了解。

The error that showed to me is: 向我显示的错误是:

 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) 

My code is: 我的代码是:

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

} }

I create all times the new mqtt because I can't create there globally. 我一直在创建新的mqtt,因为我无法在全局上创建。

Most likely what is happening is that your mqtt.publish call is happening before the mqtt.connect() has finished. 最有可能发生的情况是您的mqtt.publish调用是在mqtt.connect()完成之前发生的。 connect() is an asynchronous call and needs to complete before calling subscribe. connect()是一个异步调用,需要在调用订阅之前完成。

In your delegate methods you could call your publish once you get confirmation that the service has connected. 在委托方法中,一旦确认服务已连接,就可以调用publish

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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