2017-04-13 62 views
0

獲取的參數轉換後標籤錯誤斯威夫特:
參數標籤「(字節長度:, :)」不匹配任何可用的重載參數標籤錯誤

有效載荷分配:
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) 
    } 

建議?

+2

是否有你爲什麼不使用'zpl.data(使用:.utf8)'將'String'轉換爲'Data'的原因? – FelixSFD

+1

@FelixSFD =)可能是因爲我很新的菲利克斯。我會看看那個。感謝您的建議。向你們學習很多。 – JLanders

回答

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) 
+1

它編譯...但嘗試用一些字符串,然後'打印(有效載荷爲NSData)' - 你會感到驚訝。 –

+0

是的,就是這樣!每次當我轉換某些東西時,從「長度」到「數量」的改變都會讓我感受到!謝謝 – JLanders

+0

Phillip。關於'textview.text = String(data,encoding:String.Encoding.utf8)'的任何建議' – JLanders