Why does arrays handle strings containing swedish

2019-06-02 05:30发布

问题:

Ok, my problem is that whenever i collect data from the parser into an array where the string contains Swedish ÅÄÖ characters. In my example the

[schemaInfoArray objectAtIndex:3] 

is supposed to be @"Lördag" but is saved as @"L" and the

[schemaInfoArray objectAtIndex:4] 

contains the rest of the string that gets presented as @"ördag"

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

    {

        tempStrang = string;
        [schemaInfoArray insertObject:tempStrang atIndex:uppraknare];
        uppraknare++;



    }



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




            if ( [elementName isEqualToString:@"schemaInfo"] ) 
            {

            }
            if ( [elementName isEqualToString:@"modfromtid"] ) 
            {
                frommodarbtid = [schemaInfoArray objectAtIndex:0];
            }

            if ([elementName isEqualToString:@"modtomtid"] ) 
            {
                tommodarbtid = [schemaInfoArray objectAtIndex:1];
            }
            if ([elementName isEqualToString:@"modrast"] ) 
            {
                modrast = [schemaInfoArray objectAtIndex:2];
            }
            if ([elementName isEqualToString:@"benamning"] ) 
            {
                benamning = [schemaInfoArray objectAtIndex:3];

            }
            if ([elementName isEqualToString:@"fromnormarb"] ) 
            {
                fromnormarbtid = [schemaInfoArray objectAtIndex:4];
            }
            if ([elementName isEqualToString:@"tomnormarb"] ) 
            {
                tomnormarbtid = [schemaInfoArray objectAtIndex:5];
            }
            if ([elementName isEqualToString:@"rast"] ) 
            {
                normrast = [schemaInfoArray objectAtIndex:6];
            }       

        }

Does anyone have any thoughts about how to actually get @"Lördag" to be saved into ONE index instead of getting split into several indexes? This really destroys the structure of things that is supposed to be presented.

回答1:

This is a documented design choice from Apple, and has nothing to do with Swedish characters:

Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.

So you should do just as they say: use a NSMutableString to accumulate the results, and when the element changes, save the buffer to a permanent, (preferrably) immutable NSString.

As requested, here's an example. It was written without any kind of IDE, so chances are that it'll work, but there's no guarantee that it will either compile or work.

@interface Foo : NSObject<NSXMLParserDelegate> {
    NSMutableString* accumulator;
    NSMutableArray* schemaInfoArray;
    int uppraknare; // whatever 'uppraknare' means
}

/* snip */

@end

@implementation Foo

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string
{
    // only accumulate characters, until we get notified that we went through
    // the whole XML element
    [accumulator appendString:string];
}

-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)nsuri qualifiedName:(NSString*)qName
{
    // we went through the whole element! time to save!
    NSString* immutableResult = [accumulator copy];
    [schemaInfoArray insertObject:immutableResult atIndex:uppraknare];
    uppraknare++;
    [immutableResult release];
    // clear the accumulator for the next element
    [accumulator deleteCharactersInRange:NSMakeRange(0, [accumulator length])];

    /* the rest of your code here */
}

@end


回答2:

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string is not guaranteed to contain the complete contents of the string. You need to have a class instance variable that is a NSMutableString that can append all of foundCharacters between the calls to didStartElement and didEndElement. Inside of didEndElement add the the string to the schemaInfoArray.