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

久未见 提交于 2019-12-13 06:48:52

问题


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 for this to happen?


回答1:


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



来源:https://stackoverflow.com/questions/4107004/how-to-send-an-nsdictionary-or-plist-file-as-nsdata-using-gamekit-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!