gpt4 book ai didi

swift - NetService 无法发布

转载 作者:行者123 更新时间:2023-12-03 17:46:14 24 4
gpt4 key购买 nike

我正在尝试在 Swift 中本地发布 bonjour 服务进行简单测试。我通过一个简单的单 View 应用程序来执行此操作,该应用程序仅创建一个 netService 对象并尝试发布它。当我运行应用程序时,它无法发布,并且委托(delegate)调用 didNotPublish 函数,从而生成错误

code -72004 ("An required argument was not provided when initializing the NSNetService instance").

我无法弄清楚缺少的参数可能是什么,因为我已经指定了每个域、类型、名称和端口。

class ViewController: NSViewController, NetServiceDelegate {

var netService : NetService?

override func viewDidLoad() {
super.viewDidLoad()

//initialize the NetService object
self.netService = NetService(domain: "local.", type: "testService._tcp.", name: "netServiceTest", port: Int32(80))

//assing NetService delegate to ViewController object
self.netService!.delegate = self

//publish it
self.netService!.publish()
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}

//netservice delegate functions
func netService(_ sender: NetService, didNotPublish errorDict: [String : NSNumber]) {
print("uh oh, could not publish netService. domain:\(netService!.domain) type:\(netService!.type) name:\(netService!.name) port:\(netService!.port)")
print("error code:\(errorDict)")
}

func netServiceDidPublish(_ sender: NetService) {
print("netService published.")
}

func netServiceDidStop(_ sender: NetService) {
print("netService stopped.")
}

func netServiceWillPublish(_ sender: NetService) {
print("Service will publish, apparently")
}
}

最佳答案

您的类型设置不正确。您需要在类型前添加下划线前缀。
所以它应该看起来像这样:“_testService._tcp。”

<小时/>

来自NetService Documentation :

type
The network service type. type must contain both the service type and transport layer information. To ensure that the mDNS responder searches for services, as opposed to hosts, prefix both the service name and transport layer name with an underscore character (“_”). For example, to search for an HTTP service on TCP, you would use the type string "_http._tcp.". Note that the period character at the end of the string, which indicates that the domain name is an absolute name, is required.

关于swift - NetService 无法发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60211781/

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