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条)

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来