如果没有您的完整代码,可能很难确定您的问题是什么。我怀疑您将变量/常量声明为函数的本地变量。当它们超出范围时,对服务的引用就超出了范围。这就是为什么您尝试了一个阻塞调用来请求来自 STDIN 的输入(以使事情停留在那里)。根据 Apple 文档,netService 和 netServiceBrowser 都隐式地与默认运行循环关联,因此您也不需要显式地这样做。与运行循环显式关联会导致程序卡住,这不是您想要的。此代码创建以下输出
netServiceWillPublish:<NSNetService 0x14522e00> local _helloworld._tcp. hello
netServiceBrowserWillSearch
netServiceDidPublish:<NSNetService 0x14522e00> local. _helloworld._tcp. hello
netServiceDidFindService
并且不会被阻塞或处于阻止程序正常运行的运行循环中。在 AppDelegate.swift 中
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var nsns:NSNetService?
var nsnsdel:BMNSDelegate?
var nsb:NSNetServiceBrowser?
var nsbdel:BMBrowserDelegate?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let BM_DOMAIN = "local"
let BM_TYPE = "_helloworld._tcp."
let BM_NAME = "hello"
let BM_PORT : CInt = 6543
/// Netservice
nsns = NSNetService(domain: BM_DOMAIN,
type: BM_TYPE, name: BM_NAME, port: BM_PORT)
nsnsdel = BMNSDelegate() //see bellow
nsns?.delegate = nsnsdel
nsns?.publish()
/// Net service browser.
nsb = NSNetServiceBrowser()
nsbdel = BMBrowserDelegate() //see bellow
nsb?.delegate = nsbdel
nsb?.searchForServicesOfType(BM_TYPE, inDomain: BM_DOMAIN)
//println("press enter")
// this prevents the app from quitting instantly.
// NSRunLoop.currentRunLoop().run()
// NSFileHandle.fileHandleWithStandardInput().availableData
return true
}
以及其他地方的委托回调......
class BMNSDelegate : NSObject, NSNetServiceDelegate {
func netServiceWillPublish(sender: NSNetService!) {
println("netServiceWillPublish:\(sender)");
}
func netService(sender: NSNetService, didNotPublish errorDict: [NSObject : AnyObject]) {
println("didNotPublish:\(sender)");
}
func netServiceDidPublish(sender: NSNetService) {
println("netServiceDidPublish:\(sender)");
}
func netServiceWillResolve(sender: NSNetService) {
println("netServiceWillResolve:\(sender)");
}
func netService(sender: NSNetService, didNotResolve errorDict: [NSObject : AnyObject]) {
println("netServiceDidNotResolve:\(sender)");
}
func netServiceDidResolveAddress(sender: NSNetService) {
println("netServiceDidResolve:\(sender)");
}
func netService(sender: NSNetService, didUpdateTXTRecordData data: NSData) {
println("netServiceDidUpdateTXTRecordData:\(sender)");
}
func netServiceDidStop(sender: NSNetService) {
println("netServiceDidStopService:\(sender)");
}
func netService(sender: NSNetService,
didAcceptConnectionWithInputStream inputStream: NSInputStream,
outputStream stream: NSOutputStream) {
println("netServiceDidAcceptConnection:\(sender)");
}
}
class BMBrowserDelegate : NSObject, NSNetServiceBrowserDelegate {
func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
didFindDomain domainName: String,
moreComing moreDomainsComing: Bool) {
println("netServiceDidFindDomain")
}
func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
didRemoveDomain domainName: String,
moreComing moreDomainsComing: Bool) {
println("netServiceDidRemoveDomain")
}
func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
didFindService netService: NSNetService,
moreComing moreServicesComing: Bool) {
println("netServiceDidFindService")
}
func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
didRemoveService netService: NSNetService,
moreComing moreServicesComing: Bool) {
println("netServiceDidRemoveService")
}
func netServiceBrowserWillSearch(aNetServiceBrowser: NSNetServiceBrowser!){
println("netServiceBrowserWillSearch")
}
func netServiceBrowser(netServiceBrowser: NSNetServiceBrowser,
didNotSearch errorInfo: [NSObject : AnyObject]) {
println("netServiceDidNotSearch")
}
func netServiceBrowserDidStopSearch(netServiceBrowser: NSNetServiceBrowser) {
println("netServiceDidStopSearch")
}
}