質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2107閲覧

xcode8.0 :unexpectedly found nil while unwrapping an Optional value

sandalwalk

総合スコア77

Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/11/02 00:24

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! } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

止まったときにDebug navigatorが下画像のような感じになっていると思いますが、カーソル位置から順に下に見ていって、最初に見つかった自分のソースが止まった原因の箇所です。

Hoge

この場合だと Hoge.viewDidLoad() ということになります。(カーソルを合わせれば該当行に飛びます)

投稿2016/11/02 00:59

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問