I'm using NSNetService and want to store some data in TXTRecordData. If I just store an NSString, it works OK - but if I store a nested dictionary then dataFromTXTRecord... returns nil. For example:

NSData* d = [NSNetService dataFromTXTRecordDictionary:@{@"A": @"B"}];
// d != nil

NSData* d = [NSNetService dataFromTXTRecordDictionary:@{@"A": @{@"X":@"Y"}}];
// d == nil

Obviously I seem to be abusing TXTRecordData but I'd like to understand what's going on. I even tried to serialize my nested dictionary to a string, but it still returns nil. TXTRecordData seems very particular. Anyone know why?

有帮助吗?

解决方案

A Bonjour/DNS text record can only store a flat list of key/value pairs, not an arbitrary nested dictionary.

From DNS-SD (Rendezvous) TXT record format:

DNS-SD uses DNS TXT records to store arbitrary name/value pairs conveying additional information about the named service. Each name/value pair is encoded as it's own constituent string within the DNS TXT record, in the form "name=value". Everything up to the first '=' character is the name. Everything after the first '=' character to the end of the string (including subsequent '=' characters, if any) is the value.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top