So I've programmed with Arduino, HTML(5), CSS and Javascript before but now I'm trying to learn some Objective-C. So sorry if this question sounds stupid, I've tried to following:

MainController.m

#import "MainController.h"

@implementation MainController    

-(IBAction)start:(id)sender{
while(int x1 = 0; x1 < 100; x1++) {
    popen("echo a > /dev/tty.usbmodem621", "r");
  }
}

I get back an error: "Use of undeclared identifier x1"

So I tried declaring it in

MainController.h:

#import <Foundation/Foundation.h>

@interface MainController : NSObject {

}
extern int x1;
@end

But still I get the same errors. Could anybody tell me what I'm doing wrong? Is this even possible? Also is it possible to exchange the statement with an other IBaction? (repeating an earlier declared IBaction a 100 times)

Thanks for your time

有帮助吗?

解决方案

You declared extern variable (privat) in the @interface file and after that you declared it again in while loop. There is also mistake in while loop, it should be for:

for(int x1 = 0; x1 < 100; X1++)

the last X1 should be lowercase it makes a difference in Objective c. You should use for loop, while loop works like that:

while(CONDITION)
{
    //logic here
}
//for example
while(x1 < 100)
{
    //logic here
}

You don't have to declare

extern int x1;

the better is just

int x1;

其他提示

You use the syntax of for with while

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top