尽管有 mysql 条目,但 NSArray 在 iOS 中为空 [英] NSArray is empty in iOS despite mysql entry

查看:34
本文介绍了尽管有 mysql 条目,但 NSArray 在 iOS 中为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用来自 php 脚本的 xml 输出填充 Xcode 中的数组,以便 ios 可以连接到 mysql.但是,尽管 mysql 中有信息,但我的数组一直为空值.这一定意味着我的属性部分或我对 xml 的解析是错误的,但我真的不知道实际上是什么错误(并且没有多少主演似乎有帮助).我没有从 php 或 xcode 收到任何错误.有什么想法吗?

I have been trying to fill an array in Xcode with xml output from a php script so that ios can connect to mysql. However, I keep getting an empty value for my array despite the fact that there is information in mysql. That must mean that either my attribute section or my parsing of the xml is wrong, but I really don't know what it is that actually is wrong (and no amount of starring at it seems to help). I am not getting any errors from php or xcode. Any ideas?

我遵循了下面的建议,但我仍然得到一个空值,尽管我相信它来自我的 php...

- (void)viewDidLoad
{
    [super viewDidLoad];

    char *cStr = "YES";
    NSString *str3 = [NSString stringWithUTF8String:cStr];


    NSString *urlString = [NSString stringWithFormat:@"(censored)", _login];

    NSXMLParser *Parser = [[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
    [Parser setDelegate:self];
    [Parser parse];


NSDictionary *itemAtIndex =(NSDictionary *)[oneam objectAtIndex:0];
  oneam = [[NSMutableArray alloc] init];   
    if (([[itemAtIndex objectForKey:@"its"]isEqualToString:str3 ])) {
            [switch1 setOn:YES animated:YES];
    }
    else {
            [switch1 setOn:NO animated:YES];
    }

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    if ( [elementName isEqualToString:@"location"]) {
        [oneam addObject:[[NSDictionary alloc] initWithDictionary:attributeDict]];
    NSLog(@"The array is %@", oneam);
    }
}

这是我的 php...

And here is my php...

<?php

$login = "hello";
$dbh = new PDO('(censored)');
$sql = "SELECT oneam FROM login WHERE username = '$login'";

$q = $dbh->prepare( $sql );

$doc = new DOMDocument();
$r = $doc->createElement( "oneam" );
$doc->appendChild( $r );
foreach ( $q->fetchAll() as $row) {
    $e = $doc->createElement( "location" );

    $e->setAttribute( 'its', $row['oneam']);

    $r->appendChild( $e );

}
print $doc->saveXML();
?>

推荐答案

我不知道您是否在其他地方拥有它们,但是您缺少 2 个我在进行 .xml 解析时一直使用的方法:foundCharacters 和didEndElement.我关注了本教程集(全部 3 个视频 - 价值约 40 分钟),在几个不同的实例上使用它之后,它真的帮助我理解了如何使用 NSXMLParsers.也许尝试遵循他的方法,看看是否可以为您解决问题.

I don't know if you maybe have them somewhere else, but you're missing 2 methods I've always used when doing .xml parsing: foundCharacters and didEndElement. I followed this tutorial set (all 3 videos - about 40 min worth), and after using it on a couple different instances it really helped me understand how to use the NSXMLParsers. Maybe try following his methods and see if that clears things up for you.

这篇关于尽管有 mysql 条目,但 NSArray 在 iOS 中为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆