参数标签错误

时间:2017-04-13 13:28:09

标签: swift swift3

获取参数转换为Swift后出现标签错误:
参数标签'(字节:,长度:)'与任何可用的重载都不匹配

有效载荷分配:
let payload = Data(bytes: bytes, length: length)

        if characteristic.uuid.isEqual(CBUUID(string: TRANSFER_CHARACTERISTIC_UUID)) {
            print("Writing to characteristic \(characteristic)")
            let zpl: String = "^XA^FO50,50^ADN,36,20^FDHELLO WORLD^FS^XZ"
            let bytes = zpl.utf8
            let length: size_t = (zpl.characters.count ?? 0)

            let payload = Data(bytes: bytes, length: length)
            print("Writing payload: \(payload) length of \(length)")
            //peripheral.writeValue(payload, forCharacteristic: characteristic, type: CBCharacteristicWriteWithResponse)
            peripheral.writeValue(payload, for: characteristic, type: CBCharacteristicWriteType)
        }

以下内容:

textview.text = String(data, encoding: String.Encoding.utf8)

    if (stringFromData == "EOM") {
        // We have, so show the data,
        textview.text = String(data, encoding: String.Encoding.utf8)
        // Cancel our subscription to the characteristic
        peripheral.setNotifyValue(false, for: characteristic)
        // and disconnect from the peripehral
        centralManager?.cancelPeripheralConnection(peripheral)
    }

建议?

1 个答案:

答案 0 :(得分:0)

如果您查看文档或在Xcode中使用自动填充,您将找到该方法的bytes:count:版本。也许你想要这样的东西:

let zpl: String = "^XA^FO50,50^ADN,36,20^FDHELLO WORLD^FS^XZ"
var bytes = zpl.utf8

let payload = Data(bytes: &bytes, count: zpl.characters.count)