user1345821
user1345821

Reputation: 109

For loop that doesn't get entered

So I got this for loop in a function, but it never gets entered,

   for (Window *window in _app.windows) {
                        NSLog(@"test.");

    }

I'm a beginner so where do I start to debug this and see where it goes wrong?

EDIT This is in another class

(its in a function (loadApp) that I call in my ViewController, like this: self.app = [MyClass loadApp]; , the above code is also in my ViewController.

        Window *window = [[Window alloc] initWithName:title subtitle:subtitle number:number ident:ident type:type chapternumber:chapternumber icon:icon text:text img:img question:question answerFormat:answerFormat answerLength:answerLength tip1:tip1 tip2:tip2 tip3:tip3 tip1Answer:tip1Answer tip2Answer:tip2Answer tip3Answer:tip3Answer];

    [app.windows addObject:window];

}

return app;

Upvotes: 0

Views: 82

Answers (2)

JiuJitsuCoder
JiuJitsuCoder

Reputation: 1876

You have to make sure you are accessing the same variable. That is the gist of all the other comments and answers you are getting. It needs to be setup something like this. Keep in mind, your app may not be setup exactly like this. This is just a general structure to follow:

//myViewController.h
#import "WindowClass.h"
#import "AppClass.h"
@property (strong, nonatomic) AppClass *app;


//myViewController.m
#import "myViewController.h"
@synthesize app;

(id)init....{
   //...init code here
   //Synthesized objects must be initialized before they are accessed!
   self.app = [[AppClass alloc] init];
   return self;
}
(void)loadApp {
   WindowClass *aWindow = [[WindowClass alloc] init];
   [self.app.windowArray addObject:aWindow];
   return;
}
(void)loopFunction {
   for (WindowClass *window in self.app.windowArray) {
      NSLog(@"test.");
   }
   return;
}

//AppClass.h
@property (strong, nonatomic) NSArray *windowArray;

//AppClass.m
#import "AppClass.h"
@synthesize windowArray;

Upvotes: 0

Ege Akpinar
Ege Akpinar

Reputation: 3286

Try the following

if(!_app)  {
  NSLog(@"app is nil");
}
else if(!_app.windows) {
  NSLog(@"windows is nil");
}
else  {
  NSLog(@"there are %d windows", [_app.windows count]);
}

I suspect you'll see there are 0 windows

Upvotes: 2

Related Questions