gpt4 book ai didi

swift - HMHomeManager 返回零

转载 作者:行者123 更新时间:2023-11-28 05:53:06 25 4
gpt4 key购买 nike

我正在基于 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() 返回 nil,我不明白为什么真的......有什么想法吗?非常感谢!

最佳答案

来自 Apple 文档:

You don’t create homes directly — instead, you create them with the addHome(withName:completionHandler:) method of HMHomeManager.

如果您的 AIAHouse 对象继承自 NSObject 而不是 HMHome,您的代码将被编译。

关于swift - HMHomeManager 返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52211109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com