将尝试通过打破约束来恢复<nslayoutconstraint:0x17008b130 uilabel =“”

时间:2017-02-06 10:19:39

标签: ios swift uilabel nslayoutconstraint

=“”

我正在将我的swift应用程序连接到蓝牙低能耗外设。我在Swift中很新,我的问题与快速编码完全无关,而不是BLE。

我听外围设备看是否按下了按钮。我定义了一个标签并为其分配了一个文本。当应用程序检测到按下按钮时,我希望更改标签测试,但我的应用程序崩溃了。

你可以在这里看到我的代码:

ws.iter_cols()

说:self.statusLabel.text =&#34;您的购买已注册。&#34;

是我得到例外的地方。

这是我得到的错误:

   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
    }

}

2 个答案:

答案 0 :(得分:0)

据我所知,错误在于:

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

如果你在下面看到它,似乎NSLayout试图将标签的中心宽度设置为270并因为你的文字大于270而失败。

在下面它说你的应用程序崩溃,因为你的标签实际上是零。

尝试printf(self.myLabel)查看它是否为零。

如果您在故事板中添加标签并在viewDidAppear完成之前按下按钮,则您的标签可能为零。

也可以毫无约束地尝试它。只需删除约束。 +等到ViewController加载(viewDidAppear),然后点击。

你的应用程序崩溃是因为它解包了一个nil的optinal值,而不是因为约束。

答案 1 :(得分:-1)

尝试在故事板中为该标签设置一些内容,以查看是否触发了视觉警告。我怀疑您设置了约束,假设标签具有特定的宽度,当您指定的文本字符串与故事板中的文本字符串不同时,该约束会被破坏。要解决这个问题,请尝试更改约束值以适应动态文本 - 例如将尾随约束设置为大于或等于最小填充(例如8px)。