5

I am getting a build semantic error in XCode when I attempt to build my project. I have 3 NSOperation classes setup to download information from the internet, process it, and send it to the parent view controller, an instance of ViewController. Here is the code for a working class:

#import <Foundation/Foundation.h>
#import "ViewController.h"

@interface ImageGetter : NSOperation

@property (nonatomic) int sid;
@property (nonatomic, retain) ViewController* pvc;

@end

Here is the code for the one that is not working:

#import <Foundation/Foundation.h>
#import "ViewController.h"

@interface CurrentGetter : NSOperation

@property (nonatomic) int sid;
@property (nonatomic, retain) ViewController* pvc;

@end

Error:

ERROR: Unknown type name 'ViewController'; did you mean 'UIViewController'?

Why am I getting this error? I imported ViewController.h in both files and only one is working.

ViewController.h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

#import "DataGetter.h"
#import "CurrentGetter.h"
#import "AppDelegate.h"

@interface ViewController : UIViewController <UITabBarDelegate>
{
    NSTimer* tmr;
}

@property (nonatomic) float max;
@property (nonatomic, retain) NSDictionary* plist;
@property (nonatomic, retain) NSArray* processingobj;

@property (nonatomic, retain) AVAudioPlayer* intro;
@property (nonatomic, retain) AVAudioPlayer* woop;

@property (nonatomic) float dataDone;
@property (nonatomic) BOOL introDone;
@property (nonatomic) BOOL currentDone;

@property (nonatomic) int newSid;
@property (nonatomic) int newPart;

@property (nonatomic, retain) NSMutableArray* views;

@property (nonatomic) int audioTab;
@property (nonatomic) int currentSid;
@property (nonatomic) BOOL isPlaying;

@property (nonatomic, retain) UIScrollView* partTab;
@property (nonatomic, retain) NSMutableArray* nowPlayingObj;
@property (nonatomic, retain) UINavigationBar* audioNav;

@property (nonatomic, retain) MPMoviePlayerController* player;
@property (nonatomic, retain) UILabel* elapsed;
@property (nonatomic, retain) UILabel* remaining;
@property (nonatomic, retain) UISlider* slider;
@property (nonatomic) BOOL sliderEditing;
@property (nonatomic, retain) UIButton* playBtn;

@property (nonatomic, retain) UITabBar* tabBar;

//Images

@property (nonatomic, retain) UIImage* announcement;
@property (nonatomic, retain) NSMutableArray* seriesBanners;
@property (nonatomic, retain) UIProgressView* prog;

- (void)plistDone;

@end
Liftoff
  • 24,717
  • 13
  • 66
  • 119

1 Answers1

12

Try doing a forward declaration of the class in your CurrentGetter header file, like this:

@class ViewController;
@interface CurrentGetter : NSOperation

This tells the compiler that there exists a class called "ViewController." When there are circular dependencies, like when two classes have instances of each other, using forward declarations of the classes resolves the confusion that results.

ewrobinson
  • 351
  • 4
  • 9
  • Well the build succeeded with that line. Can you explain why this worked? – Liftoff Mar 04 '13 at 17:10
  • Yeah, sorry. I'm a bit new to the site, so I forgot that it was considered good form to include explanations. Does my brief explanation make sense? – ewrobinson Mar 04 '13 at 17:12
  • So why did the ImageGetter class build properly when the CurrentGetter did not, even though they have the exact same code? – Liftoff Mar 04 '13 at 17:12
  • @David, if this worked then your ViewController.h must not be defining ViewController. – Jim Rhodes Mar 04 '13 at 17:13
  • 1
    How can I fix that? This must be some programming etiquette I'm not familiar with. – Liftoff Mar 04 '13 at 17:19
  • I just generally use forward declarations of my classes when in this situation. As far as I am aware, there is no programmatic way around it. – ewrobinson Mar 04 '13 at 17:27