创建了一个对象,但在objective-c和iOS中仍然为空



这是我的功能:

- (void) function{
    if (_app.error == nil) {  
        for (Window *window in _app.windows) {
            NSLog(@"test.");
        }
    }
    else {  
        NSLog(@"%@",[_app.error localizedDescription]);
    }
}

不是进入for循环并打印出"test.",而是从else语句中给我"(null)"。我做错了什么?

+ (App *)loadApp {
NSString *filePath = [self dataFilePath:FALSE];
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
App *app = [[App alloc] init];
if (doc == nil) app.error = error;
...etc

你只是把你的逻辑反到前面;如果没有错误(_app.error == nil):

,您想继续
- (void) function{
    if (_app.error == nil) {      // NOT !=
        for (Window *window in _app.windows) {
            NSLog(@"test.");
        }
    }
    else {  
        NSLog(@"%@",[_app.error localizedDescription]);
    }
}

最新更新