Search code examples
iosnsstringbluetoothnsdatagksession

GKSession: Sending an Array of Strings


I'd like one peer in a session to send an array of strings to another, connected peer. Is there a way to convert such an array into NSData or do I need to send the strings one after another? Thanks very much.


Solution

  • An NSArray of NSString objects is a property list. You can convert it to an NSData like this:

    NSError *error;
    NSData *data = [NSPropertyListSerialization dataWithPropertyList:myArray
        format:NSPropertyListBinaryFormat_v1_0
        options:0
        error:&error];
    

    You can convert the data back to a property list like this:

    NSError *error;
    NSArray *myArray = [NSPropertyListSerialization propertyListWithData:data
        options:NSPropertyListImmutable
        format:NULL
        error:&error];