iOS NSMutableString返回奇怪的字符

时间:2012-07-22 07:17:48

标签: ios nsmutablearray nsarray nsmutablestring

我创建了几个单词的数组,然后我将所有单词放入一个可变字符串中,然后将该字符串的每个字符的可变数组放入。但是,“wordSplitted”是一个可变的字符数组,在模拟器中每次启动app时会给出随机乱码,显示在日志中:

2012-07-22 09:12:01.108 xxx[4484:b603] wordSplitted contains: (
    "",
    "",
    "\U221e",
    "-",
    G,
    N,
    P,
    "",
    ""
)

它应该是b,i,l,h,u,k,s,k,j。

   NSArray *threeWords = [NSArray arrayWithObjects:@"bil", @"huk", @"skje", nil];


    NSMutableString *allTogether = [[NSMutableString alloc]init];

    /*
    for (NSString *s in threeWords)
    {
        [allTogether appendString:s];
    }
     */

    for (int b = 0; b < [threeWords count]; b++)
    {
        [allTogether appendString:[threeWords objectAtIndex:b]];
    }
    NSLog (@"allTogether contains %@", allTogether);

    //[threeWords release];
    [allTogether release];

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

//this function gives me headache
        for (int a = 0; a < 9; a ++)
        {
            //gives also scrambled characters

        //[wordSplitted addObject:[NSString stringWithFormat:@"%C",[allTogether characterAtIndex:a]]];

        NSRange range = {a,1};

        [wordSplitted addObject:[allTogether substringWithRange:range]];

    }

    NSLog (@"wordSplitted contains: %@", wordSplitted);

如何显示像字母一样的普通字符?

2 个答案:

答案 0 :(得分:3)

您致电[allTogether release],但稍后再参考。通常这会导致程序崩溃,但有关CFMutableArray支持的内容使得这种情况并非总能发生。

基本上,在您确定已经完成使用之前,请不要释放阵列。

答案 1 :(得分:1)