为什么数组通过使用两个或多个索引来处理包含瑞典ÅÄÖ字符的字符串?

时间:2011-03-30 12:52:16

标签: iphone objective-c xcode nsmutablearray nsxmlparser

好吧,我的问题是每当我从解析器收集数据到一个数组,其中字符串包含瑞典ÅÄÖ字符。在我的例子中

[schemaInfoArray objectAtIndex:3] 

应该是@“Lördag”,但保存为@“L”和

[schemaInfoArray objectAtIndex:4] 

包含呈现为的字符串的其余部分 @ “ö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];
            }       

        }

有没有人想过如何将@“Lördag”保存到一个索引而不是分成几个索引?这实际上破坏了应该呈现的事物的结构。

2 个答案:

答案 0 :(得分:3)

这是documented design choice from Apple,与瑞典字符无关:

  

因为 string 可能只是其中的一部分   的总字符内容   当前元素,你应该追加它   到目前的积累   元素改变之前的字符。

所以你应该像他们说的那样:使用NSMutableString来累积结果,当元素发生变化时,将缓冲区保存为永久的(最好)不可变的NSString

根据要求,这是一个例子。它是在没有任何IDE的情况下编写的,所以很有可能它会工作,但不能保证它会编译或工作。

@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

答案 1 :(得分:1)

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string不保证包含字符串的完整内容。您需要一个类实例变量,它是一个NSMutableString,可以在对didStartElement和didEndElement的调用之间附加所有的foundCharacters。在didEndElement内部,将字符串添加到schemaInfoArray

相关问题