Iphone 字符串替换字符范围为don';t更换它';s追加!

Iphone 字符串替换字符范围为don';t更换它';s追加!,iphone,objective-c,nsstring,Iphone,Objective C,Nsstring,我花了5个小时试图找到一种方法。我正在尝试为iphone制作一个hangman应用程序,下面的方法是当玩家选择一个角色并匹配所选单词时应该调用的方法 -(void)replaceTheHiddenTextWithNewText:(NSString*)character{ NSString *fullTextField = fullText.text; int textCount = [hiddenText.text length]; NSString *theRiddle; for (int

我花了5个小时试图找到一种方法。我正在尝试为iphone制作一个hangman应用程序,下面的方法是当玩家选择一个角色并匹配所选单词时应该调用的方法

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

问题是StringByReplacingCharactersRange不会替换字符,它会将其附加到下划线中我做错了什么

致以最良好的祝愿,
M Hegab刚刚玩弄了你的代码。它不起作用,但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的内容是@“\uuuuuuuuuuuuu” 全文的内容是“Kartoffelkäfer”。因此hiddentext是全文的确切长度。
我必须改变什么才能让它工作:

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;

远离良好的代码,但我试图改变你的代码尽可能少

只是在玩你的代码。它不起作用,但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的内容是@“\uuuuuuuuuuuuu” 全文的内容是“Kartoffelkäfer”。因此hiddentext是全文的确切长度。
我必须改变什么才能让它工作:

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;

远离良好的代码,但我试图改变你的代码尽可能少

很好,我会尽快尝试这个,但在我尝试你的代码之前,我需要告诉你一些事情。。我已经在3上做了除法,因为每个uuu都有空格围绕着它,所以隐藏的文本和全文永远不会是一样的。。是它的3倍。。这就是全部,但我会尝试你的代码与一个小tweek为它为我的条件,除非你有一个更好的想法,3螃蟹除。。非常感谢您的宝贵帮助,希望它能工作,但我不得不睡觉这里真的很晚了:Dgreat我会尽快尝试这个,但在我尝试您的代码之前,我需要告诉您一些事情。。我已经在3上做了除法,因为每个uuu都有空格围绕着它,所以隐藏的文本和全文永远不会是一样的。。是它的3倍。。这就是全部,但我会尝试你的代码与一个小tweek为它为我的条件,除非你有一个更好的想法,3螃蟹除。。不管怎样,谢谢你非常宝贵的帮助。希望它能起作用,但我得睡觉了。这里真的很晚了