0

我想检索在 a 中解析的元素NSMutableArray并将它们存储到一个NSString变量中,然后将它们存储在NSMutableArrayas 中NSString(因为我想在 a 中显示内容NSComboBox)。我试过这个,但它不起作用。你能解决问题吗,我无法解决:

//--this is the parsing code : 
- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"user"]) {
        NSLog(@"user element found – create a new instance of User class...");
        if(currentElementValue == nil)
            currentElementValue = [NSMutableString string];
        else 
            [currentElementValue setString:@""];
    }
    else {
        currentElementValue = nil;
    }
        user = [[User alloc] init];


}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (!currentElementValue) {
        // init the ad hoc string with the value     
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    } else {
        // append value to the ad hoc string    
        [currentElementValue appendString:string];
        if (currentElementValue) 
        {
            currentElementValue = nil;
        }
    }
    NSLog(@"Processing value for : %@", string);
}  

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"users"]) {
        // We reached the end of the XML document
        return;
        NSLog(@"QUIT");
    }
    if ([elementName isEqualToString:@"userName"]) {
        [[self user] setUserName:currentElementValue];
        NSLog(@"final step for value: %@", user.userName);
        NSLog(@"currentElementName content : %@", currentElementValue);
        [currentElementValue release];
        NSLog(@"release : %@", currentElementValue);
        currentElementValue = nil;
        NSLog(@"release : %@", currentElementValue);
    }
    if ([elementName isEqualToString:@"firstName"]) {
        [[self user] setFirstName:currentElementValue];
        [currentElementValue release];
        currentElementValue = nil;
    }

    if ([elementName isEqualToString:@"lastName"]) {
        [[self user] setLastName:currentElementValue];
        [currentElementValue release];
        currentElementValue = nil;
    }

    if ([elementName isEqualToString:@"user"]) {
        NSLog(@"\n user=%@ \n",user);

        [users addObject:user];
        NSLog(@"userName test : %@", users);

        [user release];
        user = nil;
    }
}
-(BOOL)parseDocumentWithData:(NSData *)data {

    if (data == nil)
        return NO;
    NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
    [xmlparser setDelegate:self];
    [xmlparser setShouldResolveExternalEntities:NO];

    BOOL ok = [xmlparser parse];
    if (ok == NO)
        NSLog(@"error");
    else
        NSLog(@"OK");

    [xmlparser release];
    return ok;
}

// this is the xml file : 

<users>
 <user>
  <userName>mspeller</userName>
  <firstName>Mike</firstName>
  <lastName>Speller</lastName>
 </user>
 <user>
  <userName>mgdan</userName>
  <firstName>Mila</firstName>
  <lastName>Gdan</lastName>
 </user>

</users>


//-------
NSMutableArray *tabletest= [[NSMutableArray alloc] init];
NSMutableString * result = [[NSMutableString alloc] init];
int i;
for(i=0; i < [users count]; i++){

    [result appendString:[NSString stringWithFormat:@"%@",[[users objectAtIndex:i] valueForKey:@"userName"]] ];
    NSLog(@"result==%@",result);

    [tabletest addObject:result];
}
4

2 回答 2

0

根据您在评论部分中的链接,我认为您以错误的方式访问“userName”属性。您正在尝试访问它,因为用户包含NSDictionary对象。据我所知,您正在将User对象添加到NSMutableArray.

尝试以下操作(我冒昧地对代码进行了一些美化):

NSMutableArray *tabletest= [NSMutableArray array];

for (User* user in users)
{
    NSString* result = [NSString stringWithFormat:@"%@", user.userName];
    NSLog(@"result==%@",result);

    [tabletest addObject:result];
}

如果我完全误解了您的设计,请纠正我。

于 2012-07-17T08:51:29.737 回答
0

我不遵循您的意图,但是您的代码目前所做的是将相同的字符串[user count]时间添加到数组tabletest中,如下所示:

该行:

[result appendString:[NSString stringWithFormat:@"%@",[[users objectAtIndex:i] valueForKey:@"userName"]] ];

累积成result将每个附加[[users objectAtIndex:i] valueForKey:@"userName"]在一起的结果 - 循环的每次迭代都将下一个项目添加到result.

该行:

[tabletest addObject:result];

将引用的对象result添加到数组中。每次迭代都会这样做一次,因此数组最终会[users count]引用同一个对象。将对可变字符串的引用放置到数组中不会放置其当前值的副本,而只是对字符串的引用 - 对字符串进行变异,并且通过存储在数组中的引用可以看到变异。

因此,您的代码的最终结果是对同一可变字符串的引用数组[users count],并且该字符串是所有[[users objectAtIndex:i] valueForKey:@"userName"]值的串联。

你的意图是什么?

如果您尝试创建一个字符串表示形式的数组,[[users objectAtIndex:i] valueForKey:@"userName"]则将代码更改为:

NSMutableArray *tabletest= [[NSMutableArray alloc] init];

for(int i = 0; i < [users count]; i++)
{
   // create a string representation of userName
   NSString *result = [NSString stringWithFormat:@"%@",[[users objectAtIndex:i] objectForKey:@"userName"]];
   // add the string to the array
   [tabletest addObject:result];
}

但也许你的意图是别的?

于 2012-07-17T08:53:33.513 回答