我需要将通过PHP脚本中的数组从Web接收的数据转换为可以从中提取值的数组。这是我的代码!
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//NSString *payloadAsString = [NSString stringWithUTF8String:[receivedData bytes]];
NSArray *payloadAsString = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];
[payloadAsString finishEncoding];
verified = [payloadAsString objectAtIndex:0];
NSLog(@"logging");
//NSString *no = [[NSString alloc] init stringWithCString:verified];
NSLog(@"%@", verified);
if([verified isEqualToString:@"admin"]){
NSLog(@"test admin");
[self performSelector:@selector(changeViewAdmin) withObject:nil afterDelay:0.05];
}
if([verified isEqualToString:@"user"]){
NSLog(@"test user");
[self performSelector:@selector(changeView) withObject:nil afterDelay:0.05];
}
if([verified isEqualToString:@"No"]){
NSLog(@"test no");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Invalid UserName/Password combination!"
delegate:self
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[payloadAsString release];
//NSLog(@"%@", verified);
// INSERT GOOGLE MAPS URL REQUEST HERE
/*if(requestType == 1){
NSString* addressText = payloadAsString;
// URL encode the spaces
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
// lets throw this text on the log so we can view the url in the event we have an issue
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
// */
//
//}
[connection release];
self.receivedData = nil;
}
不幸的是,我的控制台返回null并询问我是否已将-finishencoding方法放入。问题是,如果这是正确的,我会在哪里这样做?
PS:另一个问题是,如果我从数据库中检索数据数组,那么PHP脚本是最好的方法吗?谢谢。答案 0 :(得分:2)
1)在所有这些代码中,唯一与您的问题相关的字符串是
NSArray *payloadAsString = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];
我真的怀疑PHP脚本会以NSKeyedUnarchiver兼容格式返回数据。我相信你没有从这个方法获得NSInvalidArgumentException异常的唯一原因是receivedData
是nil
(你是否在任何地方初始化它?)。尝试从你收到的字符串中创建一个字符串
[[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]
并记录下来。由此我希望如何解析响应。
2)不要将NSArray实例命名为'blahBlahString'。字符串和数组完全不同。
答案 1 :(得分:0)
NSKeyedUnarchiver只能解析由NSKeyedArchiver类的实例生成的实例。