Why does arrays handle strings containing swedish ÅÄÖ characters by using two or more indexes?

老子叫甜甜 提交于 2019-12-08 03:11:48

问题


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.



来源:https://stackoverflow.com/questions/5486514/why-does-arrays-handle-strings-containing-swedish-%c3%85%c3%84%c3%96-characters-by-using-two-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!