douxu4610 2012-11-06 16:09
浏览 81
已采纳

尽管有mysql条目,NSArray在iOS中仍为空

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?

EDIT: I have followed the suggestions below and I am still getting an empty value, although I believe that it is coming from my 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);
    }
}

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条回答 默认 最新

  • dpstir0081 2012-11-06 18:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装