NSURLRequest 将 NSData 转换为数组

发布于 2024-10-20 12:35:52 字数 2003 浏览 1 评论 0原文

我需要将通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-10-27 12:35:52

1)在所有这些代码中,与您的问题相关的唯一字符串是

NSArray *payloadAsString = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];

我真的怀疑 PHP 脚本是否以 NSKeyedUnarchiver 兼容格式返回数据。我相信您没有从此方法中获得 NSInvalidArgumentException 异常的唯一原因是 receivedDatanil (您是否在任何地方初始化了它?)。尝试根据您收到的内容创建一个字符串

[[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]

并记录它。由此我希望能够清楚如何解析响应。

2) 不要将 NSArray 实例命名为“blahBlahString”。字符串和数组是完全不同的。

1) Of all this code the only string relevant to your question is

NSArray *payloadAsString = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];

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 is nil (did you initialize it anywhere?). Try to make a string from what you receive like this

[[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]

and 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.

北座城市 2024-10-27 12:35:52

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文