Unknown type name - objective-c

My .h file:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameData.h"
#import "PROBattleScene.h"
#interface PROBattleAI : NSObject {
BattleType type;
PROBattleScene *scene;
}
-(id)initWithType:(BattleType)_type andBattleInformation:(NSMutableDictionary*)_information andScene:(PROBattleScene*)_scene;
-(void)dealloc;
#end
But on the line PROBattleScene *scene; I get the unknown type name error from Xcode.
I tried the answer here: xcode unknown type name but I am already doing that (and doesn't work).
Why is that happening? I am already importing my PROBattleScene.h file, why isn't it being recognized?
EDIT: And the contents of PROBattleScene.h as requested:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameData.h"
#import "SimpleAudioEngine.h"
#import "PROBattleBackground.h"
#import "PROBattleAI.h"
#interface PROBattleScene : CCLayer {
NSMutableDictionary *battleInformation;
NSMutableArray *localPlayerPartyData;
PROBattleBackground *background;
CCNode *base;
PROBattleAI *enemyAI;
}
+(CCScene*)scene;
-(id)init;
-(void)loadBattleInformation;
-(void)loadBGM;
-(void)loadBackground;
-(void)loadBase;
-(void)loadEnemyAI;
-(void)beginBattle;
#end

You have a circular dependency. PROBattleAI imports PROBattleScene which imports PROBattleAI which imports PROBattleScene < zomg infinite loop >
Use #class PROBattleWhatever in your headers as much as possible. Only import headers for protocol definitions or superclasses.
EDIT Ok, the above wording was totally bad...and misleading. Here is what (I believe) happens in detail. Your PROBattleAI imports PROBattleScene, which then imports PROBattleAI, which then imports PROBattleScene for a second time (all before it gets to any of the code in either file). The import will ignore PROBattleScene this time because it has already been imported and you will get the undefined type error since the file was skipped.

Related

"Attempting to use the forward class 'Game' as superclass of 'MathGame'" in Cocos2d

I'm making a Cocos2d game for iphone, and I have my main game mode, Game, which inherits from CCLayer.
I'm trying to make another game mode, MathGame, which inherits from Game, but when I try to compile, I get this error in MathGame.h:
Attempting to use the forward class 'Game' as superclass of 'MathGame'
I get the error even if the implementation and interface of MathGame are empty. And it only happens if I try to include MathGame.h in another file.
Here's the code for the Game class:
// Game.h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#import "SplashScreenLayer.h"
#interface Game : CCLayer
// A bunch of stuff
#end
The new game type:
// MathGame.h
#import "Game.h"
#interface MathGame : Game
#end
And the main menu that includes both:
// SplashScreen.h
#import "cocos2d.h"
#import "Game.h"
#import "MathGame.h"
#import "HowToPlayLayer.h"
#import "AboutLayer.h"
#interface SplashScreenLayer : CCLayer
// A bunch of stuff
#end
I can't find anything helpful online. Any ideas?
You simply have an import cycle:
Game imports SplashScreenLayer
SplashScreenLayer imports MathGame
MathGame imports Game
Your solution:
Leave the import inside the MathGame, and change the other imports to #class.
To sum it up:
// Game.h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#class SplashScreenLayer;
#interface Game : CCLayer
// A bunch of stuff
#end
The new game type:
// MathGame.h
#import "Game.h"
#interface MathGame : Game
#end
And the main menu that includes both:
// SplashScreen.h
#import "cocos2d.h"
#import "HowToPlayLayer.h"
#import "AboutLayer.h"
#class Game;
#class MathGame;
#interface SplashScreenLayer : CCLayer
// A bunch of stuff
#end
With your question answered above, let me explain a few things I already know from reading about forward declerations and import cycles:
First, go read about them! They are a very important part of Objective-C, and you don't want to miss it!
Secondly, use #class whenever you need that class for private variables or method parameters. Use imports for inheritance and strong properties.
Thirdly, don't forget to #import your forwarded classes in the implementation file!
In my case,I user the xx class and use the #class but not #import the .h file.and the compile complain..

IOS duplicate interface definition

hello i have the code below on my .h file
import <UIKit/UIKit.h>
#interface NSFont : NSObject <NSCoding> {
}
#end
#interface NSParagraphStyle : NSObject <NSCoding> {
}
#end
and i get that error :
error: duplicate interface definition for class 'NSParagraphStyle'
i have no includes and no duplicates as pointed by some users
main.m imports
#import <UIKit/UIKit.h>
pref
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
NSParagraphStyle was added in the latest (unreleased) version of iOS. So you don't need to define it yourself. You probably need to change the name, don't use NS as prefix in that case.
NSParagraphStyle already exists (see here).
Any why are you using the NS namespace - if you want to creat your own classes, make your own namespace and precede your class names with that (i.e. MHParagraphStyle) so your paragraphStyle class won't clash with the built in one :)

Xcode warns about missing protocol definition, even though #protocol is used

Since I had a import-cycle recently, I'm moving all #import statements (concerning my own files) from the header into the corresponding .m-file. I also added #class and #protocol forward-declarations to soothe the compiler. However, I still get he following warning:
Cannot find the protocol definition for 'MyCustomDelegate'.
As I said, there is an #protocol MyCustomDelegate before I use it in the #interface-Block. Interestingly this warning only occurs if the corresponding delegate is declared in another file (whose header is imported in the .m-file).
I read that one solution is to declare the delegate in a separate header file and import that file directly in the header of the class that implements the delegate. Is this really the way to go? Are there any other solutions? I think those delegates already bloated our code enough, now I should go on and even declare an own file for it?
Small sample code to better illustrate the problem:
NewFooController.h
#import <UIKit/UIKit.h>
#protocol NewFooControllerDelegate;
#interface NewFooController : UITableViewController
#property (nonatomic, weak) id<NewFooControllerDelegate> delegate;
#end
#protocol NewFooControllerDelegate
#end
HomeTableViewController.h
#import <UIKit/UIKit.h>
#protocol NewFooControllerDelegate;
// warning points to line below
#interface HomeTableViewController : UITableViewController <NewFooControllerDelegate>
#end
HomeTableViewController.m
#import "HomeTableViewController.h"
#import "NewFooController.h"
#implementation HomeTableViewController
#end
HomeTableViewController.h references the protocol, but it hasn't been declared yet.
If you import NewTaskController.h in HomeTableViewController.h before it attempts to use it, it should solve your problem.
Of course you can then remove the import from HomeTableViewController.m
Not sure if this is "best way", but try import header of class that implement protocol before class header file.
HomeTableViewController.m
#import "NewFooController.h"
#import "HomeTableViewController.h"
#implementation HomeTableViewController
#end
And you can remove protocol declaration in HomeTableViewController.h
#import <UIKit/UIKit.h>
#interface HomeTableViewController : UITableViewController <NewFooControllerDelegate>
#end

#import in objective C: Am I doing this wrong?

Sorry, couldn't find a more appropriate title.
In My code I have two classes which should know of each others existence. So I use an instance variable which points to the other class. For that to work (I guess?) the other classes headers file should be imported so it knows which methods it has and such.
Here is my code (stripped down)
MainMenuController.h:
#import <Cocoa/Cocoa.h>
#import "IRCConnection.h"
#interface MainMenuController : NSViewController {
IRCConnection *ircConnection;
}
#property (strong) IRCConnection *ircConnection;
#end
IRCConnection.h:
#import <Foundation/Foundation.h>
#import "MainMenuController.h"
#interface IRCConnection : NSObject {
MainMenuController *mainMenuController;
}
#property (strong) MainMenuController *mainMenuController;
#end
As you can see they both import each other, but this creates an error (Unknown type name 'IRCConnection') in one, and in the other Unknown type name 'MainMenuController'.
However when the connection is just one way (e.g. only MainMenuController knows about IRCConnection) and thus there is only an import statement in one of the two, it works fine.
How can I have them to know about each other? In both ways.
Hope this question makes any sense.
you could remove the import from IRCConnection.h and use a #class statement instead.
like this:
#import <Foundation/Foundation.h>
#class MainMenuController;
#interface IRCConnection : NSObject {
then add a #import "MainMenuController.h" to IRCConnection.m
In the header, use forward declaration:
#class IRCConnection;
#interface MainMenuController : NSViewController {
IRCConnection *ircConnection; // ok
}
In the source file (.m), do #import.
You cannot have circular imports. You need to break them up, or introduce some forward declarations.

#protocol implementation in #interface in Objective-C

I need to develop an application which has a interface which implements methods of 3 protocols.
Assume protocol A extends protocol B and protocol C, and interface implements protocol A.
This is how my code looks,
// This is in MyClass.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_A"
#interface MyClass : NSObject <protocol_A>
{
}
#end
//This is MyClass.m file
#import "MyClass.h"
#implementation myClass
-(void)methodinA
{
NSLog(#"I'm in protocol_A");
}
}
-(void)methodinB
{
NSLog(#"I'm in protocol_B");
}
-(void)methodinC
{
NSLog(#"I'm in protocol_C");
}
#end
//This is protocol_A.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_B.h"
#import "protocol_C.h"
#protocol protocol_A <protocol_B, protocol_C>
-(void)methodinA;
#end
//This is in protocol_B.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_B
-(void)methodinB;
#end
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
-(void)methodinC;
#end
i'm getting an exception , and my app is getting crashed...
***Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyClass 0X323nm31>setvalue:forundefinedKey:]:this class is not key value coding-compilant for the key window'.
Plz Tel me how to solve this problem??
So where you're getting this from (and the reason you're getting it 3 times) is you've got a mistake in your protocol definitions. You have:
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
{
}
-(void)methodinC;
#end
You can't declare class members in a protocol: only methods. Because of this, you don't need (and, as you've discovered) can't have the curly braces in the protocol definition. As such, you need this for your protocol definitions:
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
-(void)methodinC;
#end
Removing those should solve your issue.
When making new files, I always go through Xcode's new-class-files process, as it frequently gives you lots of convenient stuff. Here is the contents of a new protocol_D declaration fresh from Xcode:
#import <Cocoa/Cocoa.h>
#protocol protocol_D
#end
Hope this helps!
TL;DR: Protocol definitions can't have curly-braces anywhere in them.
Protocols generally go in a .h file; always go in a .h file if you plan on using them anywhere.
Just like everything else, you need to #import the .h file that contains the definition of the protocol before you use it.
So, in MyClass.h (it really should be capitalized -- Classes are always capitalized in Objective-C), #import the various protocol .h files.
Your protocol_A.h file declares conformance to protocol_B and protocol_C, yet you haven't imported the headers for protocol_B and protocol_C. This means that you are declaring conformance to protocols that as far as the compiler is concerned, don't exist in protocol_A.h. You need to import the headers:
In protocol_A.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_B.h" //note these new imports
#import "protocol_C.h"
#protocol protocol_A <protocol_B, protocol_C>
-(void)methodinA;
#end
Also see Apple's Communicating with Objects, which discusses delegates, protocols, and selectors. Though its listed under Mac OS X, most (if not all) appears to apply to iOS also.