Невозможно подписать значение типа «[String: CBPeripheral]» с индексом типа «Int». - PullRequest
0 голосов
/ 12 октября 2019

Я не могу установить основной ярлык ячейки на имя соответствующего периферийного устройства.

помогите мне найти решение !!

описание изображения здесь

var activeCentralManager: CBCentralManager?
var peripheralDevice: CBPeripheral?
var devices: Dictionary<String, CBPeripheral> = [:]
var deviceName: String?
var devicesRSSI = [NSNumber]()
var devicesServices: CBService!
var deviceCharacteristics: CBCharacteristic!  

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return devices.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Let's get a cell.
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    // Turn the device dictionary into an array.
    let discoveredPeripheralArray = devices as? [String : CBPeripheral]
    //println(discoveredPeripheralArray.count)

    // Set the main label of the cell to the name of the corresponding peripheral.
    if let cell = cell{
        if let name = discoveredPeripheralArray?[indexPath.row].name{
            if let textLabelText = cell.textLabel{
                textLabelText.text = name
            }
            if let detailTextLabel = cell.detailTextLabel{
                detailTextLabel.text = devicesRSSI[indexPath.row].stringValue
            }
        }
    }

    return cell!
}

Ответы [ 2 ]

1 голос
/ 12 октября 2019

Проблема здесь:

// Turn the device dictionary into an array.
let discoveredPeripheralArray = devices as? [String : CBPeripheral]

[String : CBPeripheral] не является массивом. Это словарь. Так что discoveredPeripheralArray тоже словарь. И вы не можете волшебным образом превратить словарь в массив.

0 голосов
/ 12 октября 2019

Замените строку ошибки следующим:

let discoveredPeripheralArray = devices.keys.sorted

Вам необходимо убедиться, что devicesRSSI соответствует тому же порядку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...