gpt4 book ai didi

ios - 游戏套件和 iPhone

转载 作者:行者123 更新时间:2023-11-29 11:24:37 24 4
gpt4 key购买 nike

我将我的应用程序设置为在 p2p 连接期间以数组的形式向另一个人发送自定义级别。接收设备将数组保存到文件中以备后用。我在我的应用程序中设置了 gamekit,它将成功搜索并连接到另一台设备,没有任何问题。虽然当我向设备发送数据时出现问题,但接收设备将接收数据(并按应有的方式保存自定义级别)但它会立即崩溃。

这是我用来发送和接收数据的方法。

-(void) sendDataToPeers:(NSData *) data
{
if (currentSession)
{
//send the data
[self.currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];

//Alerting the user that the custom level has been sent.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Your custom level has been sent." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
}

-(void) btnSend
{
//Data that will be sent
NSMutableData *theData = [NSMutableData data];

//Archiver
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData];

//Desired level to send
int theLevel =[[CTManager sharedInstance]getLevel];

//Path to the custom levels
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *customLevelsSen = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"customLevels"]];

//Custom levels array
NSArray *theLevels = [[NSArray alloc] initWithContentsOfFile: customLevelsSen];

//Gets the desired level array from array of custom levels
NSArray *myArray = [[NSArray alloc]initWithArray:[theLevels objectAtIndex:theLevel-51]];

//prepare data
[archiver encodeObject:myArray forKey:@"level"];
[archiver finishEncoding];

//send the data
[self sendDataToPeers:theData];

//cleanup
[archiver release];
[theLevels release];
[myArray release];

}

-(void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context
{
//Archiver
NSKeyedUnarchiver *archiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

//Gets the custom level in form of an array from data.
NSArray *level = [archiver decodeObjectForKey:@"level"];
[archiver finishDecoding];
[archiver release];

//Path to the array of custom levels
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *customLevelsRec = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"customLevels"]];

//Gets the array of custom levels
NSMutableArray *customLevelArray = [[NSMutableArray alloc] initWithContentsOfFile:customLevelsRec];

//Adds a new array to the array of custom levels
[customLevelArray addObject:level];

//Saves the array.
[customLevelArray writeToFile:customLevelsRec atomically:YES];

//cleanup
[customLevelArray release];

//Message saying a custom level has been recieved
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Received!" message:@"A custom level has been saved." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];

}

测试这个很痛苦,因为我目前没有自己的两台设备,所以我将测试版发送给我的 friend ,他反过来测试它们(他有 ipod 和 iphone)。感谢您提供任何帮助...

如果我找不到问题,我很可能会将整个 xcode 项目发送给他,并通过屏幕共享与他计算机上的项目一起工作,以高效地构建和测试应用程序。而且我将能够使用 Debug模式。

最佳答案

我不知道你是否找到了这个问题的答案,我希望你找到了。但如果您没有,我强烈建议您尝试新 SDK 的新功能。他们没有经历整个编码/解码过程,而是通过让您执行以下操作(在您的发送方法中)使其变得简单:

data = [NSKeyedArchiver archivedDataWithRootObject:anObject];

其中 anObject 几乎可以是任何对象、数组、字典等等...

在您的接收方法中:

NSObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];

其中对象也可以是几乎任何对象。

至于您遇到的崩溃,您是否已验证崩溃发生在哪条线路上?您确定它发生在您发布的代码中吗?或者它发生在其他地方?

关于ios - 游戏套件和 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2266656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com