无法为索引类型为“ Int”的“ [String:CBPeripheral]”下标

时间:2019-10-12 13:15:10

标签: swift xcode bluetooth

我无法将单元的主标签设置为相应外围设备的名称。

帮我找到解决办法!

image description here

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 个答案:

答案 0 :(得分:1)

问题在这里:

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

[String : CBPeripheral]不是数组。这是一本字典。因此discoveredPeripheralArray也是字典。而且,您无法神奇地将字典转换为数组。

答案 1 :(得分:0)

用以下内容替换错误行:

WITH q1 AS (SELECT DISTINCT Count(*) FROM TableAudit WHERE 
(ColumnName = 'Col1' AND NewValue = 'True') OR
(ColumnName = 'Col2' AND NewValue = 'P')
Group by DatabaseAuditId
)

SELECT count(*) FROM q1

不过,您需要确保let discoveredPeripheralArray = devices.keys.sorted 匹配相同的顺序。