Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c StringByReplacingOfstring不发生';我不能接受多个词_Objective C_Nsstring - Fatal编程技术网

Objective c StringByReplacingOfstring不发生';我不能接受多个词

Objective c StringByReplacingOfstring不发生';我不能接受多个词,objective-c,nsstring,Objective C,Nsstring,因此,我有以下代码行: text = [text stringByReplacingOccurrencesOfString:@"%@" withString:[NSString stringWithString:name]]; [NSString stringWithString:name]可以防止单字符串异常 然而,现在我的问题是,如果有人输入一个超过一个单词的名称,即name=@“John Doe”,整行失败。我觉得这是一个非常简单的解决办法,但我一生都无法理解。有人知道怎么回事吗 编辑:

因此,我有以下代码行:

text = [text stringByReplacingOccurrencesOfString:@"%@" withString:[NSString stringWithString:name]];
[NSString stringWithString:name]
可以防止单字符串异常

然而,现在我的问题是,如果有人输入一个超过一个单词的
名称,即
name=@“John Doe”,整行失败。我觉得这是一个非常简单的解决办法,但我一生都无法理解。有人知道怎么回事吗

编辑:包括错误行周围的代码。

此函数存储名称:

- (void) buttonSelected:(UIButton *)button
{
NSString *text;
if ([button.titleLabel.text isEqualToString:@"NEXT"]  || [button.titleLabel.text isEqualToString:@"DEBATE BACK"] || [button.titleLabel.text isEqualToString:@"ISSUE A CLOTURE"])
{
    bool noText = true;
    bool foundTextBox = false;
    for (UIView *view in [scrollView subviews])
    {
        if (view.tag == 51)
        {
            if ([view isKindOfClass:[UITextField class]])
            {
                foundTextBox = true;
                UITextField *blah = (UITextField *)view;
                text = blah.text;
                NSLog(@"%@", text);
                if (![text isEqualToString:@""] && text != nil) noText = false;
            }
        }
    }
    if (!foundTextBox) noText = false;
    if (!noText)
    {
        if (questionNumber == 0)        name = text;
        else if (questionNumber == 1)   state = text;
        else if (questionNumber == 2)   billSubject = text;
        [self hideKeyboard];
        [UIView animateWithDuration:0.8 animations:^
         {
             for (UIView *view in [scrollView subviews])
             {
                 CGPoint origin = CGPointMake(-10 - (view.frame.size.width/2), view.frame.origin.y+(view.frame.size.height/2));
                 [view setTag:view.tag + 100];
                 [view setCenter:origin];
                 [view setAlpha:0.0];
             }
         }
         completion:^(BOOL finished)
         {
             for (UIView *view in [scrollView subviews])
             {
                 [view removeFromSuperview];
             }
             [self nextQuestion];
         }];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"You have not entered any text!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show]; [alert release];
    }
}
}
现在我想起来了,
NSLog(@“%@”,text)从未实际打印。甚至连一个空行都没有。但是,在查找文本框的for循环之后,
noText
仍然为false

下面是对
名称的调用:

- (void) setText:(NSString *) text label:(UILabel *) label
{
if (questionNumber == 1)
{
    NSLog(@"%@", name);
    text = [text stringByReplacingOccurrencesOfString:@"%@" withString:[NSString stringWithString:name]];
    label.text = text;
}
else
    label.text = text;
}
name
会打印整个名称


注意:
questionNumber
name
state
billSubject
都是全局变量。

[text stringByReplacingOccurrencesOfString:@“某些字符模式”和string:name]

(您希望替换的“某些字符模式”是什么?它真的是“%@”?或者可能只是“@”,或者其他什么?)

如果foundTextBox设置为true!foundTextBox将为false,而noText将保持true。只有当foundTextBox为false时,noText才设置为false


(这是不必要的混乱逻辑。)

所以我设法解决了这个问题,尽管它不是一个完美的解决方案。我将
name
state
billSubject
组合成一个
NSMutableArray
来存储它们。现在,它可以回忆一个单词的所有大小写,无论是一个字符还是一个字符串中的10个单词。不知道全局
NSString
有什么问题,但此问题已解决。

为什么要替换%@?我认为您可能会与StringWithFormat混淆您是否尝试过不使用
stringWithString
?Just
text=[text stringByReplacingOccurrencesOfString:@“%@”with string:name]
您真的想将%@替换为name变量值吗?“[NSString stringWithString:name]可以防止单字符串异常。”???????你从哪里得到的?你在这里描述的情况听起来不像是程序正常工作。当字符串是一个字符或两个单词时,你的应用程序不应该崩溃。一定有一些根本问题。它真的是“%@”。字符串是“Welcome,Representative%@。您代表的是哪个州?”我正在尝试用它们的名称替换%@。只需使用
[text StringByReplacingOccurrencesOfString:@“%@”with string:name]
。如果它不处理“多个单词”,那是因为“多个单词”实际上从未进入“名称”中,可能是因为你从任何输入中解析它们的方式。输出给了我键入的全名,所以它一定是这样的parsing@tokan_one-然后将全名替换到字符串中。你应该发布你的代码(它的主要部分),因为很明显你在搞砸什么。例如,你声称stringWithString以某种方式防止单个字符引起问题几乎肯定是假的。我在原始问题中为你添加了代码块。
bool noText = true;
bool foundTextBox = false;
for (UIView *view in [scrollView subviews])
{
    if (view.tag == 51)
    {
        if ([view isKindOfClass:[UITextField class]])
        {
            foundTextBox = true;
            UITextField *blah = (UITextField *)view;
            text = blah.text;
            NSLog(@"%@", text);
            if (![text isEqualToString:@""] && text != nil) noText = false;
        }
    }
}
if (!foundTextBox) noText = false;