stringByReplacingCharactersInRange不替换它的追加!

时间:2010-11-06 13:50:37

标签: iphone objective-c nsstring

我花了5个小时尝试找到一个方法..我正在尝试为iphone做一个刽子手应用程序,下面的方法是当玩家选择一个角色并且它与所选单词匹配时应该调用的方法..

-(void)replaceTheHiddenTextWithNewText:(NSString*)character{
NSString *fullTextField = fullText.text;
int textCount = [hiddenText.text length];

NSString *theRiddle;
for (int i = textCount-1 ; i>=0; i--) {

    NSString *hiddenTextField = [[NSMutableString alloc] initWithString:hiddenText.text];
    NSString *aChar=[fullTextField substringWithRange:NSMakeRange(i/3,1)];

    if ([aChar isEqualToString:@" "]) {

        theRiddle= [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@" "];
    }else if ([aChar isEqualToString:character]) {
        theRiddle =[hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:aChar];

      }else{
        theRiddle = [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"_"];


    }
    hiddenTextField = theRiddle;    
}
hiddenText.text=theRiddle;

}

问题是stringByReplacingCharactersInRange不替换字符,它将它附加到下划线我在这里做错了什么?

最诚挚的问候, M Hegab

1 个答案:

答案 0 :(得分:5)

刚刚使用了您的代码。它不起作用,但stringByReplacingCharactersInRange不是你的问题 你的游戏逻辑不能像它应该的那样工作。拿一支笔和一张纸,然后“手动”循环你的for循环,看看这一定是错的 下次,如果你盯着代码半小时,拿一支笔。这将为您节省至少4个小时: - )

您的代码存在一些问题。假设Kartoffelkäfer是您要查找的字词,用户输入字母f

for (int i = textCount-1 ; i>=0; i--) {
    NSString *hiddenTextField = [[NSMutableString alloc] initWithString:hiddenText.text];
    // you are creating this string in every loop from the text of a (I guess) UITextField. 
    // I don't know what the content of this text is but I guess it is suppossed to be `______________`
    // in every loop you replace the word where you replaced the _ with the correct letter with the string from the textfield. 
    // Btw, you are leaking this string. 

    NSString *aChar=[fullTextField substringWithRange:NSMakeRange(i/3,1)];
    // Kartoffelkäfer has 14 chars so i is 13. And 13/3 is 4. And the character at index 4 is o
    // In the next loop i is 12. And 12/3 is 4, too.
    // next three loops will give you index 3. Then you get three times index 2, and so one. 
    // you never reach the letter f, anyway. 

    if ([aChar isEqualToString:@" "]) {
        theRiddle= [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@" "];
    }else if ([aChar isEqualToString:character]) {
        theRiddle =[hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:aChar];
      }else{
        theRiddle = [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"_"];
    // You should not replace a unmatched character with a _ . Because already matched letters would be overwritten. 

    }
    hiddenTextField = theRiddle;    
}

我认为hiddenText.text的内容是@“ _ __ _ __ ” 而fullText.text的内容是@“Kartoffelkäfer”。所以hiddentext是fullText的确切长度 我必须改变才能使其发挥作用:

NSString *theRiddle;
NSString *hiddenTextField = [[[NSMutableString alloc] initWithString:hiddenText.text] autorelease];
for (int i = textCount-1 ; i>=0; i--) {
    NSString *aChar=[fullTextField substringWithRange:NSMakeRange(i,1)];
    if ([aChar isEqualToString:@" "]) {
        theRiddle= [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@" "];
    }else if ([aChar isEqualToString:character]) {
        theRiddle =[hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:aChar];
    }
    else {
        theRiddle = hiddenTextField;
    }
    hiddenTextField = theRiddle;    
}
hiddenText.text=theRiddle;

远离好的代码,但我试图尽可能少地更改代码。