2013-02-26 34 views
0

循环,使我得到这个,用于在功能循环,但它永远不会进入,对于没有得到进入

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

    } 

我是初学者所以我在哪里开始调试这个,看看它出错了?

EDIT 这是另一个类

(其在一个函数(loadApp),我打电话在我的ViewController,像这样:self.app = [MyClass的loadApp],上面的代码还在

 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; 
+2

您正试图对数组执行快速枚举。如果你的表达式'_app.windows'是零或者是空的,你将永远不会进入循环。 – trudyscousin 2013-02-26 17:54:10

+0

我做了一个简单的if(_app == nil),它不是零 – user1345821 2013-02-26 17:56:30

+1

然后'windows'必须是空的,即不包含任何元素。 – trudyscousin 2013-02-26 17:57:04

回答

2

我的ViewController。请尝试以下

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

我怀疑你会看到有0瓦特indows

+0

是的,它告诉我有0个窗口,但我在另一个类中添加了窗口对象,请检查我的帖子,然后使用代码对其进行编辑。 – user1345821 2013-02-26 18:03:08

+1

你初始化了Windows吗?所以像app.windows = [[NSMutableArray alloc] init]。还要检查你传入的窗口对象是不是零?也许你忘记了在你的初始化方法中“回归自我”? – 2013-02-26 18:06:20

+0

我已经在App类的init方法中返回self。同样在App类中,我做了self.windows = [[NSMutableArray alloc] init]; – user1345821 2013-02-26 18:08:05

0

您必须确保您访问的是相同的变量。这是您获得的所有其他评论和答案的要点。它需要设置这样的东西。请记住,您的应用可能没有完全像这样设置。这只是一个通用的结构:

//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;