我正在基于HomeKit创建自己的“ HouseKit”。该API将使我能够非常快速地执行homekit动作,例如light.on()。
按照我的解释,然后在此处介绍我的问题,这是我的灯光管理公共班级的一个示例。效果很好。
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。
这是我创建家庭的完整课程:
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()
返回nil,但我不明白为什么真的...知道吗?提前非常感谢!
答案 0 :(得分:1)
从Apple文档中获取:
您不直接创建房屋,而是使用
addHome(withName:completionHandler:)
的{{1}}方法。
如果您的HMHomeManager
对象继承自AIAHouse
而不是NSObject
,则您的代码将编译。