为什么数组通过使用两个或多个索引来处理包含瑞典语ÅÄÖ字符的字符串? [英] Why does arrays handle strings containing swedish ÅÄÖ characters by using two or more indexes?

查看:43
本文介绍了为什么数组通过使用两个或多个索引来处理包含瑞典语ÅÄÖ字符的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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] 

应该是@Lördag",但另存为@"L"和

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

[schemaInfoArray objectAtIndex:4] 

包含显示为的其余字符串@ördag"

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];
            }       

        }

有人对如何将@Lördag"实际保存到一个索引而不是拆分成几个索引有任何想法吗?这确实破坏了本应呈现的事物的结构.

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.

推荐答案

这是

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

因为 string 可能只是其中的一部分的总字符内容当前元素,您应该附加它到目前的积累直到元素更改为止.

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.

因此,您应该按照他们所说的去做:使用 NSMutableString 累积结果,并且当元素更改时,将缓冲区保存到永久的(最好是)不可变的 NSString 代码>.

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.

根据要求,这里有一个例子.它是在没有任何IDE的情况下编写的,因此很有可能会运行,但是不能保证它会编译或运行.

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

这篇关于为什么数组通过使用两个或多个索引来处理包含瑞典语ÅÄÖ字符的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆