0

For the project I am working on (using Swift, not Object-C!!!) I need to convert a UInt16 that I extracted from a BLE characteristic to a string, so I can use it as text for a label. Anyone out there who already has solved this problem?

Thanks for your help in advance.

var currentValue: UInt16!

func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
   if characteristic.UUID == xgatt_io_p_1 {
      let data = characteristic.value
      let reportData = UnsafePointer<UInt16>(data.bytes)
      currentValue = reportData.memory
   }
   readValue()
}

label.text = convertedCurrentValue

1 Answers1

0

You can do this:

label.text = "\(convertedCurrentValue)"
Greg
  • 25,317
  • 6
  • 53
  • 62
  • Doesn't work under Swift (iOS-App)... I gave it a try in playground - no prob at all! Damn it! What am I to do now? – Markus Schlüter Jan 26 '15 at 08:16
  • Try in in xCode project not playground and let me know what error will you get. It should work. – Greg Jan 26 '15 at 08:44
  • I get: fatal error: unexpectedly found nil while unwrapping an Optional value – Markus Schlüter Jan 26 '15 at 08:58
  • So the problem is not in the code I provided you in the answer. convertedCurrentValue is nil, check the code where you are assigning value to this variable and make sure it's not nil. Or make this optional (?) and do: if let val = convertedCurrentValue {label.text = "\\(val)"} this will print the value just if it's not nil – Greg Jan 26 '15 at 09:05
  • Yes, but look at this: func printValue() { //works println(currentValue) } func returnValue() -> UInt16{ //doesn't work return currentValue } //why? – Markus Schlüter Jan 26 '15 at 09:17
  • It can be many things what cause that issue. It's difficult to tell without seeing who picture. – Greg Jan 26 '15 at 09:26
  • You are right :-P but I recognised that the problem is of a different nature - I am doing something wrong while trying to pass the value - I'll tell you as soon as I have found the solution to this :-/ – Markus Schlüter Jan 26 '15 at 09:27
  • Hi Greg, thanks for trying to help me: I recognised after a while that I have been trying to access the ViewController from another class what generated the problem in the first place. After reconstructing most of it everything went fine. Thank you again :-) – Markus Schlüter Jan 27 '15 at 08:00