From d275d79d5de1ac918965c25932e72f0485ac3e98 Mon Sep 17 00:00:00 2001 From: Jeff Johnson Date: Wed, 13 Mar 2019 18:12:46 -0500 Subject: [PATCH] Fix for https://github.com/lapcat/Bonjeff/issues/10 Crash when viewing ipfs daemon --- source/ServiceDelegate.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/ServiceDelegate.swift b/source/ServiceDelegate.swift index 11def38..915db71 100644 --- a/source/ServiceDelegate.swift +++ b/source/ServiceDelegate.swift @@ -116,7 +116,13 @@ class ServiceDelegate:NSObject, NetServiceDelegate, BonjourNode { records.removeAll() if let txtRecordData = service.txtRecordData() { - let txtRecord = NetService.dictionary(fromTXTRecord:txtRecordData) + // This crashes in Swift: + //let txtRecord = NetService.dictionary(fromTXTRecord:txtRecordData) + // When the TXT Record is not in the format key=value, + // +[NSNetService dictionaryFromTXTRecordData:] illegally inserts NSNull in place of NSData, + // and this crashes when bridged to Swift because it can't bridge NSNull to Data. + // We fall back to using the TXT Record data itself with no key. + let txtRecord = CFNetServiceCreateDictionaryWithTXTData(nil, txtRecordData as CFData)?.takeRetainedValue() as? Dictionary ?? ["":txtRecordData] for (key, data) in txtRecord { if let line = NSString(data:data, encoding:String.Encoding.utf8.rawValue) { records.append( (key, line as String) )