开发者

How to send an NSDictionary (or plist file) as NSData using GameKit bluetooth?

开发者 https://www.devze.com 2023-01-24 04:27 出处:网络
I\'m trying to use [currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];

I'm trying to use

[currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];

to send an NSDictionary (or a plist file) containing data from 1 device to another, and

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context

to receive it. How can I convert to and from NSData fo开发者_开发知识库r this to happen?


You can use the following to transform Plist to NSData:

+ (NSData *)dataWithPropertyList:(id)plist
                          format:(NSPropertyListFormat)format
                         options:(NSPropertyListWriteOptions)opt
                           error:(NSError **)error

And this to transform NSData to Plist:

+ (id)propertyListWithData:(NSData *)data
                   options:(NSPropertyListReadOptions)opt
                    format:(NSPropertyListFormat *)format
                     error:(NSError **)error

of NSPropertyListSerialization class

format could be NSPropertyListBinaryFormat_v1_0 to use lowest byte count

0

精彩评论

暂无评论...
验证码 换一张
取 消