我非常感谢立即回复,我向您提交了我的xml解析器方法,最后提到了问题。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if([elementName isEqualToString:@"alluser"])
{
objUser = [[Users alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"username"])
{
objUser.userName =[currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",objUser.userName);
}
if([elementName isEqualToString:@"password"])
{
objUser.passWord = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",objUser.passWord);
}
if([elementName isEqualToString:@"user"])
{
[usersArray addObject:objUser];
[objUser release];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(!currentElementValue)
{
currentElementValue = [[NSMutableString alloc] initWithString:string];
[currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
else
{
[currentElementValue appendString:string];
[currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
if([currentElementValue isEqualToString:@"User"])
{
[currentuser appendString:string];
}
if([currentElementValue isEqualToString:@"Pass"])
{
[currentpass appendString:string];
}
}
-(IBAction)backgroundclick:(id)sender
{
[txtpass resignFirstResponder];
[txtusername resignFirstResponder];
}
-(IBAction)returnPressed:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)LoginClicked:(id)sender
{
int cnt=0;
NSString *currUser = txtusername.text;
// NSString *passWord = txtpass.text;
// int i=0;
for(Users *objUser in usersArray )
{
/// how to get value and how to compare string...
}
return;
}
我的mutable数组在这段代码中是(usersArray)。我有解析数据到数组,我想检查用户名和密码与textfields值,如果它与数组匹配,执行特定的操作。但不知何故,我无法将数据存入字符串。
答案 0 :(得分:0)
你没有详细说明问题是什么,所以只是猜测......对于你在LoginClicked()中的循环,你可以使用:
进行比较[currUser isEqualToString:objUser.userName]
或任何其他字符串比较方法。但也许你的问题是别的吗?在你的“parser:didStartElement ...”方法中你的意思是检查“user”元素而不是“allUser”吗?因为它只分配一个Users对象,但尝试解析多个并将它们插入到数组中。