NSURLRequest 将 NSData 转换为数组
我需要将通过 PHP 脚本中的数组从网络接收的数据转换为可以从中提取值的数组。这是我的代码!
-(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 脚本是最好的方法吗?谢谢。
I need to convert data received from the web via an array in a PHP script into an array that I can pull values out of. Here's my code!
-(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;
}
Unfortunately, my console returns null and asks if I've put the -finishencoding method in. Question is, if that's correct, where would I do so?
PS: Another question, is if I'm retrieving an array of data from a database, is a PHP script the best way to go? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)在所有这些代码中,与您的问题相关的唯一字符串是
我真的怀疑 PHP 脚本是否以 NSKeyedUnarchiver 兼容格式返回数据。我相信您没有从此方法中获得 NSInvalidArgumentException 异常的唯一原因是
receivedData
为nil
(您是否在任何地方初始化了它?)。尝试根据您收到的内容创建一个字符串并记录它。由此我希望能够清楚如何解析响应。
2) 不要将 NSArray 实例命名为“blahBlahString”。字符串和数组是完全不同的。
1) Of all this code the only string relevant to your question is
I really doubt that PHP script returns you data in NSKeyedUnarchiver-compatible format. I believe the only reason you don't get NSInvalidArgumentException exception from this method is that
receivedData
isnil
(did you initialize it anywhere?). Try to make a string from what you receive like thisand log it. From this I hope it will be clear how to parse response.
2) Do not name NSArray instances like 'blahBlahString'. Strings and arrays are completely different.
NSKeyedUnarchiver 只能取消存档由 NSKeyedArchiver 类的实例生成的实例。
https://developer.apple.com /library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSKeyedUnarchiver_Class/index.html
NSKeyedUnarchiver can only unarchive instances which are produced by instances of the NSKeyedArchiver class.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSKeyedUnarchiver_Class/index.html