Skip to content

Instantly share code, notes, and snippets.

Last active December 18, 2017 22:57
What would you like to do?
Adds a `fp_TXTRecord` category to NSNetService to allow for more natural access to TXT record data.
#import <Foundation/Foundation.h>
@interface FPNetServiceTXTRecord : NSObject
@property NSNetService *netService;
- (NSString *)objectForKeyedSubscript:(NSString *)key;
@interface NSNetService (FPNetServiceAddtions)
- (FPNetServiceTXTRecord *)fp_TXTRecord;
#import "FPNetServiceTXTRecord.h"
#import <objc/runtime.h>
@implementation FPNetServiceTXTRecord
- (NSString *)objectForKeyedSubscript:(NSString *)key {
NSData *data = [NSNetService dictionaryFromTXTRecordData:self.netService.TXTRecordData][key];
return (data != nil) ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
@implementation NSNetService (FPNetServiceAddtions)
- (FPNetServiceTXTRecord *)fp_TXTRecord {
FPNetServiceTXTRecord *txtRecord = objc_getAssociatedObject(self, @selector(fp_TXTRecord));
if(!txtRecord) {
txtRecord =;
txtRecord.netService = self;
objc_setAssociatedObject(self, @selector(fp_TXTRecord), txtRecord, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return txtRecord;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment