HMHomeManager 返回零
HMHomeManager returning nil
我正在基于 HomeKit 创建自己的 "HouseKit"。 api 将允许我非常快速地执行 homekit 操作,例如 light.on()。
按照我的解释,然后介绍我的问题,这里是我的public class 灯光管理示例。这很好用。
public class AIALight: AIAEquipment {
// MARK: - Initialization
public init(kind: Kind, name: String) {
super.init(.light, equipmentName: name)
}
}
extension AIALight: AIAControllable {
public enum Order: AIAOrder {
// MARK: - Values
case on
case off
var info: (Int) {
switch self {
case .on:
return 1
case .off:
return 0
}
}
}
}
然后创建一盏灯...
var myLight = AIALight(kind: .light, name: "Sdb1")
...并在需要时发送订单:
self.myLight.set(.on, completion: nil)
好的,现在保持相同的想法,我希望能够创建房屋和房间。
对于家庭 HMHomeManagerDelegate,当然还有 HMHomeManager。
这里是我完全自己class创建的主页:
import Foundation
import HomeKit
public final class AIAHouse: HMHome, HMHomeManagerDelegate {
public var house: HMHome!
public let houseName: String
public var manager: HMHomeManager? {
didSet {
self.manager?.delegate = self
}
}
// MARK: - Initialization
public init(houseName: String) {
self.manager = nil
self.houseName = houseName
}
public func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
//We browse HMHome to find CruzHouse already set via Home App on iOS
for home in manager.homes as [HMHome]{
if home.name == self.houseName{
print("Found the expected home: \(self.houseName) ")
self.house = home
}
else {
print("\(self.houseName) not found ")
}
}
}
}
然后在我的 ViewController 中:
var homeManager: HMHomeManager!
var myHome: AIAHouse!
override func viewDidLoad() {
super.viewDidLoad()
self.homeManager = HMHomeManager()
self.myHome = AIAHouse(houseName: "MyHome")
self.myHome.manager = HMHomeManager()
}
我的问题是 self.myHome.manager = HMHomeManager()
returns 没有,我不明白为什么真的......有什么想法吗?提前致谢!
来自 Apple 文档:
You don’t create homes directly — instead, you create them with the
addHome(withName:completionHandler:)
method of HMHomeManager
.
如果您的 AIAHouse
对象继承自 NSObject
而不是 HMHome
。
,您的代码将会编译
我正在基于 HomeKit 创建自己的 "HouseKit"。 api 将允许我非常快速地执行 homekit 操作,例如 light.on()。
按照我的解释,然后介绍我的问题,这里是我的public class 灯光管理示例。这很好用。
public class AIALight: AIAEquipment {
// MARK: - Initialization
public init(kind: Kind, name: String) {
super.init(.light, equipmentName: name)
}
}
extension AIALight: AIAControllable {
public enum Order: AIAOrder {
// MARK: - Values
case on
case off
var info: (Int) {
switch self {
case .on:
return 1
case .off:
return 0
}
}
}
}
然后创建一盏灯...
var myLight = AIALight(kind: .light, name: "Sdb1")
...并在需要时发送订单:
self.myLight.set(.on, completion: nil)
好的,现在保持相同的想法,我希望能够创建房屋和房间。 对于家庭 HMHomeManagerDelegate,当然还有 HMHomeManager。
这里是我完全自己class创建的主页:
import Foundation
import HomeKit
public final class AIAHouse: HMHome, HMHomeManagerDelegate {
public var house: HMHome!
public let houseName: String
public var manager: HMHomeManager? {
didSet {
self.manager?.delegate = self
}
}
// MARK: - Initialization
public init(houseName: String) {
self.manager = nil
self.houseName = houseName
}
public func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
//We browse HMHome to find CruzHouse already set via Home App on iOS
for home in manager.homes as [HMHome]{
if home.name == self.houseName{
print("Found the expected home: \(self.houseName) ")
self.house = home
}
else {
print("\(self.houseName) not found ")
}
}
}
}
然后在我的 ViewController 中:
var homeManager: HMHomeManager!
var myHome: AIAHouse!
override func viewDidLoad() {
super.viewDidLoad()
self.homeManager = HMHomeManager()
self.myHome = AIAHouse(houseName: "MyHome")
self.myHome.manager = HMHomeManager()
}
我的问题是 self.myHome.manager = HMHomeManager()
returns 没有,我不明白为什么真的......有什么想法吗?提前致谢!
来自 Apple 文档:
You don’t create homes directly — instead, you create them with the
addHome(withName:completionHandler:)
method ofHMHomeManager
.
如果您的 AIAHouse
对象继承自 NSObject
而不是 HMHome
。