-1

我正在制作一个 iOS 代码解释器。我已经完成了所有检查,但到目前为止您只能输入一个命令。我希望用户能够在 UITextView 中输入多个命令。我计划对文本视图执行的操作是将每一行传递给我的 IF 语句行。

有谁知道一种说法,将每一行逐个传递到 if 语句行?

- (IBAction)runCommad:(id)sender {

    //Alert
    NSString *alertCheck = @"alert(";
    NSRange alertCheckRange = [code.text rangeOfString : alertCheck];
    //Logger
    NSString *logCheck = @"log(";
    NSRange logCheckRange = [code.text rangeOfString : logCheck];

    if (alertCheckRange.location != NSNotFound) {
//If the compiler sees alert()...
        NSString *commandEdit;
        commandEdit = code.text;
        commandEdit = [commandEdit stringByReplacingOccurrencesOfString:@"alert(" withString:@""];
        commandEdit = [commandEdit stringByReplacingOccurrencesOfString:@")" withString:@""];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Syn0" message:commandEdit delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

    }else  if (logCheckRange.location != NSNotFound) {
        //If the compiler sees log()...
        NSString *commandEdit;
        commandEdit = code.text;
        commandEdit = [commandEdit stringByReplacingOccurrencesOfString:@"log(" withString:@""];
        commandEdit = [commandEdit stringByReplacingOccurrencesOfString:@")" withString:@""];
        logFile = [NSString stringWithFormat:@"%@\n%@", logFile,commandEdit];
        logTextView.text = logFile;
    }
}
4

2 回答 2

1

有两个建议,首先,如果您对块感到满意,您可以使用NSString's:

(void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block

此方法将调用块,按顺序将原始字符串中的每一行传递给它。stop如果您想在处理设置为的每一行之前停止YES

或者,您可以使用NSString's:

(NSArray *)componentsSeparatedByString:(NSString *)separator

这会将字符串分解为基于separator. 在for枚举中使用它:

for(NSString *nextLine in [originalString componentsSeparatedByString:@"\n"])
{
   // process nextLine, break from loop to stop before processing each line
}
于 2012-05-18T02:18:45.907 回答
1

首先,让您的字符串组件进行评估:

NSString *text = [textView text];    
NSArray *components = [text componentsSeperatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

您不能将开关与字符串一起使用,因此您需要使用 if 检查每种情况:

for (NSString *string in components)
{
    if ([string isEqualToString:@"The first string you're matching"])
    {
        //Do something because you found first string
    }

    if([string isEqualToString:@"The second string you're matching"])
    {
        //Do something else
    }
}

这就是想法。

于 2012-05-18T02:25:45.263 回答