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

时间:2017-12-04 03:27:33

标签: ios swift xcode cocoa mqtt

我想创建一个简单的应用程序来切换带有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,因为我不能在全球范围内创建。

1 个答案:

答案 0 :(得分:0)

最有可能的情况是您的$.fn.moveIt = function(){ var $window = $(window); var instances = []; $(this).each(function(){ instances.push(new moveItItem($(this))); }); window.addEventListener('scroll', function(){ var scrollTop = $window.scrollTop(); instances.forEach(function(inst){ inst.update(scrollTop); }); }, {passive: true}); } var moveItItem = function(el){ this.el = $(el); this.speed = parseInt(this.el.attr('data-scroll-speed')); }; moveItItem.prototype.update = function(scrollTop){ this.el.css('transform', 'translateY(' + -(scrollTop / this.speed) + 'px)'); }; // Initialization $(function(){ $('[data-scroll-speed]').moveIt(); }); 通话是在mqtt.publish完成之前发生的。 mqtt.connect()是异步调用,需要先完成,然后再调用订阅。

在您的委托方法中,一旦确认服务已连接,就可以调用connect()

publish