2017-02-06 3 views
0

Я подключаю свое быстрое приложение к периферийному устройству с низкой энергией Bluetooth. Я очень новичок в Swift, и моя проблема полностью связана с быстрой кодировкой, а не с BLE.Попытаться восстановить с помощью ограничения ограничений <NSLayoutConstraint: 0x17008b130 UILabel

Я слушаю периферийное устройство, чтобы увидеть, нажата ли кнопка. Я определил метку и присвоил ей текст. когда приложение обнаруживает нажатие кнопки, я хочу изменить тест метки, но вместо этого приложение падает.

вы можете увидеть мой код здесь: self.statusLabel.text =:

import CoreBluetooth 
import UIKit 

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 

    var manager:CBCentralManager! 
    var peripheral:CBPeripheral! 
    @IBOutlet weak var statusLabel: UILabel! 


    let BEAN_NAME = "Security Tag" 
    let BEAN_SCRATCH_UUID = CBUUID(string: "00001C0F-D102-11E1-9B23-000EFB0000A7") 
    let BEAN_SERVICE_UUID = CBUUID(string: "00001c00-d102-11e1-9b23-000efb0000a7") 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     manager = CBCentralManager(delegate: self, queue: nil) 
    } 

    func centralManagerDidUpdateState(_ central:CBCentralManager) { 
     if central.state == CBManagerState.poweredOn { 
      central.scanForPeripherals(withServices: nil, options: nil) 
      debugPrint("Searching ...") 
     } else { 
      debugPrint("Bluetooth not available.") 
     } 
    } 

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
     let device = (advertisementData as NSDictionary).object(forKey: CBAdvertisementDataLocalNameKey) as? NSString 

     if device?.contains(BEAN_NAME) == true { 
      debugPrint("Found Bean.") 

      self.manager.stopScan() 
      self.peripheral = peripheral 
      self.peripheral.delegate = self 

      manager.connect(peripheral, options: nil) 
     } 
    } 

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
     debugPrint("Getting services ...") 
     peripheral.discoverServices(nil) 
    } 

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
     for service in peripheral.services! { 
      let thisService = service as CBService 

      debugPrint("Service: ", service.uuid) 

      if service.uuid == BEAN_SERVICE_UUID { 
       debugPrint("Using scratch.") 
       peripheral.discoverCharacteristics(nil, for: thisService) 
      } 
     } 
    } 

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
     debugPrint("Enabling ...") 

     for characteristic in service.characteristics! { 
      let thisCharacteristic = characteristic as CBCharacteristic 

      debugPrint("Characteristic: ", thisCharacteristic.uuid) 

      if thisCharacteristic.uuid == BEAN_SCRATCH_UUID { 
       debugPrint("Set to notify: ", thisCharacteristic.uuid) 

       // Prepare to show data 

       self.peripheral.setNotifyValue(true, for: thisCharacteristic) 
      } 
     } 
    } 

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { 
     if characteristic.uuid == BEAN_SCRATCH_UUID { 
      let content = String(data: characteristic.value!, encoding: String.Encoding.utf8) 

      debugPrint("Notified.") 
      self.statusLabel.text = "Your Purchase is Registered." 

     } 
    } 

    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { 
     debugPrint("Disconnected.") 

     central.scanForPeripherals(withServices: nil, options: nil) 

     // Hide respective user interface 
    } 

} 

линия, которая говорит: "Ваша покупка регистрации."

, где я получаю исключение.

и это ошибки я получаю:

2017-02-06 13:42:19.825869 kevinbletest[2143:695095] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x17008b130 UILabel:0x11dd0cbf0'Waiting for a Purchase Co...'.width == 270 (active)>", 
    "<NSLayoutConstraint:0x17008b770 UILabel:0x11dd0cbf0'Waiting for a Purchase Co...'.centerX == UIView:0x11dd0c850.centerX (active)>", 
    "<NSLayoutConstraint:0x17008b7c0 UILabel:0x11dd0cbf0'Waiting for a Purchase Co...'.leading == UIView:0x11dd0c850.leadingMargin + 50 (active)>", 
    "<NSLayoutConstraint:0x17408c3f0 'UIView-Encapsulated-Layout-Width' UIView:0x11dd0c850.width == 375 (active)>" 
) 
    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x17008b130 UILabel:0x11dd0cbf0'Waiting for a Purchase Co...'.width == 270 (active)>  
    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
    fatal error: unexpectedly found nil while unwrapping an Optional value 
    2017-02-06 13:42:25.299886 kevinbletest[2143:695095] fatal error: unexpectedly found nil while unwrapping an Optional value 
    (lldb) 
+1

«Неустранимая ошибка: неожиданно обнаружена нуль при развертывании необязательного значения», это ваша проблема. –

+1

Я думаю, вы смотрите на неправильную вещь. У вас есть проблема с вашими ограничениями. Но это должно повлиять только на ваш макет и не разбивать приложение. Поскольку @NileshPol сказал выше меня здесь, у вас есть значение «Необязательное», которое вы пытаетесь развернуть, значение которого равно «nil». Это приведет к сбою приложения. Моя ставка заключается в том, что ваш 'statusLabel' не связан в вашем раскадровке. –

+1

точно, как вы заявили, что вы получаете исключение в строке (self.statusLabel.text = "yourText"), это означает, что розетка не подключена в раскадровке. Здесь ваше приложение рушится –

ответ

0

Из того, что я прочитал ошибку лежит здесь:

<NSLayoutConstraint:0x17008b130 UILabel:0x11dd0cbf0'Waiting for a Purchase Co...'.width == 270 (active)> 

Если вы читали его вниз, кажется, что NSLayout пытается центрировать метку с шириной 270 и не так ваш текст больше 270.

Далее в нем говорится, что ваше приложение выходит из строя, потому что вы наклейки фактически ноль.

Попробуйте printf(self.myLabel), чтобы узнать, есть ли оно.

Ваш ярлык может быть нулем, если вы добавили его в раскадровку, и вы нажмете кнопку до того, как закончите viewDidAppear.

Также попробуйте его без каких-либо ограничений. Просто удалите ограничения. + Подождите, пока загрузится ViewController (viewDidAppear), а затем нажмите.

Приложение вылетает из-за того, что оно разворачивает оптическое значение, которое равно нулю, а не из-за ограничений.

-1

Попытки установить некоторый контент для этой метки в раскадровке, чтобы увидеть, если это вызывает визуальное предупреждение. Я подозреваю, что вы установили ограничения, предполагающие, что метка имеет определенную ширину, которая нарушается, когда вы назначаете текстовую строку, отличную от той, что у вас есть в вашем раскадровке. Чтобы решить эту проблему, попробуйте изменить значение ограничения для размещения динамического текста - например, установите ограничение трейлинга больше или равно минимальному заполнению (например, 8px).

Смежные вопросы