For循环错误预期标识符或'('

dun*_*sys 0 for-loop objective-c

当我尝试进行for循环时,编译器给出了一个错误.

for (int i = 0; i < 26; i++) { //Expected identifier or '(', highlights the word for
NSLog(@"Test");
}
Run Code Online (Sandbox Code Playgroud)

编辑:

以下是它之前的代码:

#import "editCodeTable.h"

@implementation editCodeTable

NSArray *languages;

NSArray *everything;
Run Code Online (Sandbox Code Playgroud)

MrH*_*hma 6

你似乎对如何编程一般感到困惑......你不能让代码只是挥之不去地围绕着所有"不知不觉".您需要将for循环放在适当的方法或函数中.

例如,我认为你这样做(如果我理解正确的话):

#import "editCodeTable.h"

@implementation editCodeTable

NSArray *languages;

NSArray *everything;

for (int i = 0; i < 26; i++) { //Error here!
    NSLog(@"Test");
}

@end
Run Code Online (Sandbox Code Playgroud)

您需要将代码放在方法或函数中,然后在需要它的任何地方调用方法/函数来打印测试.例如,您可以这样做:

#import "editCodeTable.h"

@implementation editCodeTable

NSArray *languages;

NSArray *everything;

void printTest() //This is a C function -> C code is perfectly 
                 //acceptable in Objective-C
{
    for (int i = 0; i < 26; i++)
    {
        NSLog(@"Test");
    }
}

//Or you could do this:

- (void) printOutTest //This is an Objective-C method
{
    for (int i = 0; i < 26; i++)
    {
        NSLog(@"Test");
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅Objective-C指南或参考书.您不能只在任何地方放置代码.您需要根据适当的语法对其进行组织.但是,如果没有关于最终目标的更多信息,我无法为您提供更具体的答案,说明您在实例中需要完成的工作.