条件绑定的初始化程序必须具有可选类型

时间:2017-02-10 03:39:07

标签: swift

我想创建一个BLE扫描程序,并在developer.apple.com上关注开始开发iOS应用程序(Swift),

当会话"创建表视图"时,我在这里收到错误

BLEMember.swift

import UIKit

class BLEMember {
    var rssi: Int
    var uuid: String

    init (rssi:Int, uuid:String){
      self.rssi = rssi
      self.uuid = uuid
    }
}

BLEScanTableViewController.swift

...
    private func loadBLEMembers() {

    var members = [BLEMember]()
    guard let member1 = BLEMember(rssi: 65, uuid: "testing ble 1") else {
        fatalError("Unable to instantiate meal1")
    }

    guard let member2 = BLEMember(rssi: 35, uuid: "testing ble 2") else {
        fatalError("Unable to instantiate meal2")
    }

    guard let member3 = BLEMember(rssi: 45, uuid: "testing ble 3") else {
        fatalError("Unable to instantiate meal2")
    }

    members += [member1, member2, member3]
}

并发生错误,

 initializer for conditional binding must have optional type, not "BLEMember"

如何解决?谢谢!

1 个答案:

答案 0 :(得分:3)

let member1 = BLEMember(rssi: 65, uuid: "testing ble 1")总是成功的。你不需要在这里使用警卫!

private func loadBLEMembers() {

    var members = [BLEMember]()
    let member1 = BLEMember(rssi: 65, uuid: "testing ble 1")

    let member2 = BLEMember(rssi: 35, uuid: "testing ble 2")

    let member3 = BLEMember(rssi: 45, uuid: "testing ble 3")

    members += [member1, member2, member3]

}