Skip to content

Instantly share code, notes, and snippets.

@shalyf
Created August 3, 2017 09:05
Show Gist options
  • Save shalyf/114004431b51b626f43dc291656843c9 to your computer and use it in GitHub Desktop.
Save shalyf/114004431b51b626f43dc291656843c9 to your computer and use it in GitHub Desktop.
解析 Bonjour 的 TXTRecord 数据
func resolveDictionary(fromTXTRecord data: Data) -> [String: String] {
var dict = [String: String]()
var offset = 0
let bytes = data.bytes
while offset < bytes.count {
let recordLength = Int(bytes[offset])
offset += 1
print(recordLength)
if recordLength > 0 {
let subBytes = bytes[offset..<offset + recordLength]
let compoments = subBytes.split(separator: 61)
if compoments.count == 2 {
let key = Data(compoments[0]).utf8StringValue
let value = Data(compoments[1]).utf8StringValue
print("key: \(key), value: \(value)")
dict[key] = value
}
}
offset += recordLength
}
return dict
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment