1

我正在基于 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。

这是我自己创建 Home 的完整课程:

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 ,我不明白为什么真的......知道吗?提前非常感谢!

4

1 回答 1

1

从苹果文档:

您不直接创建房屋 - 相反,您 addHome(withName:completionHandler:)使用HMHomeManager.

如果您的AIAHouse对象继承自NSObject而不是HMHome.

于 2018-09-07T11:37:38.903 回答