Question

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

Was it helpful?

Solution

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;

OTHER TIPS

You use the syntax of for with while

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top