title: Sending data via bluetooth with a button in Swift
body: <p>I'm trying to send data via bluetooth with a button click. I'm able to send the data when opening the app with the code below: </p>
  
  <pre><code>var manager: CBCentralManager!
  var device: CBPeripheral?
  var characteristics: [CBCharacteristic]?
  var serviceUUID = "1234"
  var char1 = "FFE1"
  let deviceName = "HMSoft"
  
  func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
      DidDiscoverChar.text = "Characteristic found!"
  
      device = peripheral
      characteristics = service.characteristics
  
      var value: UInt8 = 1
  
      let data = NSData(bytes: &amp;value, length: MemoryLayout&lt;UInt8&gt;.size)
  
      for characteristic in service.characteristics as [CBCharacteristic]!
      {
          if(characteristic.uuid.uuidString == "FFE1")
          {
              device?.writeValue(data as Data, for: characteristic,type: CBCharacteristicWriteType.withoutResponse)
  
          }
      }
  }
  </code></pre>
  
  <p>But when I try to send it with a button: </p>
  
  <pre><code>@IBAction func ledOn(_ sender: AnyObject) {
      var value: UInt8 = 1
      let data = NSData(bytes: &amp;value, length: MemoryLayout&lt;UInt8&gt;.size)
  
      device?.writeValue(data as Data, for: characteristics,type: CBCharacteristicWriteType.withoutResponse)
  }
  </code></pre>
  
  <p>I get the error:</p>
  
  <blockquote>
    <p>Cannot convert value of type '[CBCharacteristic]?' to expected argument type 'CBCharacteristic'</p>
  </blockquote>
  
  <p>It seems like an easy task, but as a newbie I'm stuck.</p>
  
  <p>EDIT: (Response to rmaddy´s answer)</p>
  
  <p>as you suggested, it did work iterating through the array, shown below.</p>
  
  <pre><code>@IBAction func ledOn(_ sender: AnyObject) {
  
      var value: UInt8 = 1
  
      let data = NSData(bytes: &amp;value, length: MemoryLayout&lt;UInt8&gt;.size)
  
  
      for characteristic in characteristics as [CBCharacteristic]!
      {
  
          if(characteristic.uuid.uuidString == "FFE1")
          {
              device?.writeValue(data as Data, for: characteristic,type: CBCharacteristicWriteType.withoutResponse)
  
          }
      }
  }
  </code></pre>
  
  <p>As I want to improve as a programmer, I want the code to be more efficient. So I thought I could do the iteration outside the button function to avoid the iteration every time I push the button. to illustrate I did a simple example in playground:</p>
  
  <pre><code>var characteristicFFE1: String = ""
  var array = ["r1", "r2", "r3", "FFE1", "r4"]
  
  for uuidString in array {
      if uuidString == "FFE1" {
         characteristicFFE1 = uuidString
      }
  }
  </code></pre>
  
  <p>And then use characteristicFFE1 directly in writeValue in IBAction like this: </p>
  
  <pre><code>@IBAction func ledOn(_ sender: AnyObject) {
  var value: UInt8 = 1
  let data = NSData(bytes: &amp;value, length: MemoryLayout&lt;UInt8&gt;.size)
  
  device?.writeValue(data as Data, for: characteristicsFFE1,type: CBCharacteristicWriteType.withoutResponse)
  }
  </code></pre>
  
  <p>I tried to accomplish the above, as shown below:</p>
  
  <pre><code>var characteristicFFE1: CBCharacteristic?
  
   override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  
      for characteristic in characteristics as [CBCharacteristic]!  //ERROR HERE
      {
  
          if(characteristic.uuid.uuidString == "FFE1")
          {
              characteristicFFE1 = characteristic
  
          }
      }
  </code></pre>
  
  <p>but I get the error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.</p>
  
  <p>anyone know how to get around this? Ive read about optionals and unwrapping, but still confused here.</p>