Created
August 3, 2017 09:05
-
-
Save shalyf/114004431b51b626f43dc291656843c9 to your computer and use it in GitHub Desktop.
解析 Bonjour 的 TXTRecord 数据
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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