xcode8.0 + iOS10の環境で、一つの画面上に、2つのtableviewを配置し、一つにはBLEの名前とUUIDを表示。もう一つのtableviewにはBLEが提供するサービスの一覧を表示するアプリを作っていますが、
print("scan stopped")
が表示された後にunexpectedly found nil while unwrapping an Optional valueのエラーが出てしまいます。
手探りでswiftを勉強中なので、このエラーは良く遭遇します。具体的にどの場所でnilが問題となっているのかを調べる方法を教えて下さい。よろしくお願いいたします。
以下がプログラムの全文です。
import UIKit import CoreBluetooth class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource,CBCentralManagerDelegate,CBPeripheralDelegate { var myUuids: NSMutableArray = NSMutableArray() var myNames: NSMutableArray = NSMutableArray() var myPeripheral: NSMutableArray = NSMutableArray() var myServices: NSMutableArray = NSMutableArray() var manager: CBCentralManager! var peripheral: CBPeripheral! @IBOutlet weak var tableViewOutlet: UITableView! @IBOutlet weak var serviceTableOutlet: UITableView! override func viewDidLoad() { super.viewDidLoad() self.manager = CBCentralManager(delegate: self, queue: nil) } @IBAction func startButton(_ sender: AnyObject) { self.manager.scanForPeripherals(withServices: nil, options: nil) print("start scan peripherals") } func centralManagerDidUpdateState(_ central: CBCentralManager) { if central.state == CBManagerState.poweredOn { print("BLE Powered ON") } } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { print("peripheral discovered") myNames.add(peripheral.name!) myPeripheral.add(peripheral) myUuids.add(peripheral.identifier.uuidString) // reload DispatchQueue.main.async(execute: { self.tableViewOutlet.reloadData() }) self.peripheral = peripheral self.peripheral.delegate = self self.manager.connect(peripheral, options: nil) self.manager.stopScan() print("scan stopped") } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { print("connected to peripheral") self.peripheral.discoverServices(nil) } func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { if error != nil { print("Failed to discover services") } else{ let services = peripheral.services print("service discovered! Discovered services are: \(services)") myServices = services as! NSMutableArray DispatchQueue.main.async(execute: { self.tableViewOutlet.reloadData() }) } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { var cellLength:Int? if tableView == tableViewOutlet { cellLength = myPeripheral.count } if tableView == serviceTableOutlet { cellLength = myServices.count } return cellLength! } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell:UITableViewCell? if tableView == self.tableViewOutlet { let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier:"firstTable" ) // Cellに値を設定. cell.textLabel?.sizeToFit() cell.textLabel?.textColor = UIColor.red cell.textLabel?.text = "\(myNames[indexPath.row])" cell.textLabel?.font = UIFont.systemFont(ofSize: 18) // Cellにsubtitleの値を設定 cell.detailTextLabel?.text = "\(myUuids[indexPath.row])" cell.detailTextLabel?.font = UIFont.systemFont(ofSize: 12) } if tableView == self.serviceTableOutlet { let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier:"serviceTable" ) cell.textLabel?.sizeToFit() cell.textLabel?.text = "\(myServices[indexPath.row])" cell.textLabel?.font = UIFont.systemFont(ofSize: 12) } return cell! } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。