Protocol error. Cannot find protocol declaration - iphone

We have error: cannot find protocol declaration for 'ClassWhichUseMainScene' [3]
We created file:
Protocol.h
#import "ScoreSystem.h"
#import "OtherSystem"
#import "OtherSystem2"
#class ScoreSystem;
#protocol SceneDelegate <NSObject>
#property (nonatomic, readonly,retain) ScoreSystem* score;
#property (nonatomic, readonly,retain) OtherSystem* system;
#property (nonatomic, readonly,retain) OtherSystem2* system2;
#end
And use in ScoreSystem.h
#import "Protocol.h"
#import "OtherSystem"
#import "OtherSystem2"
#interface ScoreSystem: NSObject <SceneDelegate>
{
OtherSystem* system;
OtherSystem2* system2;
}
In ScoreSystem we want use just OtherSystem and OtherSystem2 objects. In OtherSystem use ScoreSystem and OtherSystem2, etc.
We want create universal protocol for all system.

You have a circular dependency between your two header files (each imports the other). Do not import ScoreSystem.h in Protocol.h, the #class forward declaration is enough. The same goes for your other two imports.
As a general rule I avoid including class header files in other class header files - I just use #class everywhere and import the headers in the implementation files.

Mine Problem resolved with simple import statement
#import "ProtocolContainingFile.h"

Related

Unable to include a class into another in IOS

I wish to create object of one class into another
I have 2 classes
MyviewControler
Checkout
I want to import Checkout into MyviewController
#import "Checkout.h"
#interface MyViewController : UIViewController <UIImagePickerControllerDelegate>
{
Checkout *checkout;
}
#property (nonatomic) Checkout *checkout;
#end
It is giving me error "Unknown type name checkout"
In MyViewController.h, before #interface add:
#class Checkout;
In MyViewController.m, add:
#import "Checkout.h"
you probably have a dependency cycle. use a forward declaration, which tells the compiler there is a class with that name without needing to see its declaration:
#class Checkout; // << the forward declaration
#interface MyViewController : UIViewController <UIImagePickerControllerDelegate>
{
Checkout *checkout;
}
#property (nonatomic) Checkout *checkout;
#end
// MyViewController.m
...
#import "Checkout.h"
forward declarations are preferred in the majority of cases. the exception to this is when there is a physical dependency (e.g. the superclass' declaration should precede the subclass'). forward declarations are good because they significantly reduce the build times and the complexity of include graphs and dependency.
good luck
If the error really is as you say:
Unknown type name checkout
(note the small 'c') then the problem is that you're using checkout as a type name instead of Checkout somewhere in your code.
import the file in MyViewController.m file too.
//in .m file
#import "Checkout.h"
#class Checkout; //this was missing
Also, give the property like this.
#property(nonatomic, retain) Checkout* checkout
and synthesize it in .m file

Objective C Properties

I tried searching for this issue, but can't seem to find out what I'm doing wrong.
Here is my controller header:
#import <UIKit/UIKit.h>
#interface BabyLearnViewController : UIViewController {
UIButton *btnImage;
MediaManager* myMediaManager;
}
#property (nonatomic, retain) IBOutlet UIButton *btnImage;
#property (retain) MediaManager* myMediaManager;
- (IBAction)setNewImage;
#end
Here is my controller class:
#import "BabyLearnViewController.h"
#import "MediaManager.h";
#implementation BabyLearnViewController
#synthesize btnImage;
#synthesize myMediaManager;
I am getting the errors:
error: expected specifier-qualifier-list before 'MediaManager'
error: no declaration of property 'myMediaManager' found in the interface
Any ideas? Usually the 1st error comes up if you have a cylical reference. 'MediaManager' doesn't reference anything else. Any ideas?
Since you have no mentions of MediaManager class at the moment it is used in header file compiler can't figure out what "MediaManager" is and issues an error. Declare that class using forward declaration in your header file to let compiler know that MediaManager is actually a class:
#class MediaManager;
#interface BabyLearnViewController : UIViewController {
...
P.S. As an alternative you can import MediaManager.h in your header, but using forward declaration is preferred.
Place #import "MediaManager.h"
this in header file of BabyLearnViewController
try add #class MediaManager; before #interface

Problem in Importing File

I am importing File "FieldActivityViewController.h".But Showing an error
"/Users/rupeshnandha/Downloads/AQMD
Release 3/Classes/InputChoice.h:22:0
Expected specifier-qualifier-list
before 'FieldActivityViewController'
in /Users/rupeshnandha/Downloads/AQMD
Release 3/Classes/InputChoice.h"
As is a code Written in "InputChoice.h"
#import "FieldActivityViewController.h"
#protocol InputChoiceProtocol <NSObject>
#required
-(void) inputChoiceSelectedIndex :(int) index;
#end
#interface InputChoice : UIViewController {
//NSString *keyString;
FieldActivityViewController *field_act;
NSMutableArray *selectionArray;
IBOutlet UITableView *table;
NSObject<InputChoiceProtocol> *delegate;
// IBOutlet UIPickerView *picker;
}
#property (nonatomic,retain) NSArray *selectionArray;
#property (nonatomic,retain) FieldActivityViewController *field_act;
#property (nonatomic,retain) NSObject<InputChoiceProtocol> *delegate;
#end
It's not the good practice to import the application classes in .h files.
Try with forward class declaration and import you FieldActivityViewController file in .h
Use with forward class declaration #class FieldActivityViewController; at the place of your import statement.
use #class FieldActivityViewController
Most likely cause is that you have a circular dependency. InputChoice.h imports FieldActivityViewController.h and FieldActivityViewController.h imports InputChoice.h (not necessarily directly).
The best solution is to move the imports out of the header file into the .m file and put
#class FieldActivityViewController;
in InputChoice.h and
#class InputChoice
in FieldActivityViewController.h (or whichever header has InputChoic.h in it.

Error when declaring NSManagedObjectContext

I'm trying to create a NSManagedObjectContext object. They error reads as follows:
Expected specifier-qualifier-list
before 'NSManagedObjectContext'
and here is my header file:
#import <UIKit/UIKit.h>
#interface FavouritesViewController : UITableViewController {
NSArray *favourites;
NSManagedObjectContext *context;
}
#property (nonatomic, retain) NSArray *favourites;
#property (nonatomic, retain) NSManagedObjectContext *context;
#end
Anyone know I might be missing here?
Most probably you have forgotten to include the CoreData header in your file. Right after the line #import <UIKit/UIKit.h> you need another line that reads #import <CoreData/CoreData.h>. After this the file should compile fine. Also make sure that you have CoreData in your linked libraries, otherwise you will get runtime errors.
You need to add #class NSManagedObject above your interface directive. This will tell the compiler that NSManagedObject is a real class. You then need to have #import <CoreData/CoreData.h> in your .m file.

Adding C++ Object to Objective-C Class

I'm trying to mix C++ and Objective-C, I've made it most of the way but would like to have a single interface class between the Objective-C and C++ code. Therefore I would like to have a persistent C++ object in the ViewController interface.
This fails by forbidding the declaration of 'myCppFile' with no type:
#import <UIKit/UIKit.h>
#import "GLView.h"
#import "myCppFile.h"
#interface GLViewController : UIViewController <GLViewDelegate>
{
myCppFile cppobject;
}
#end
However this works just fine in the .mm implementation file (It doesn't work because I want cppobject to persist between calls)
#import "myCppFile.h"
#implementation GLViewController
- (void)drawView:(UIView *)theView
{
myCppFile cppobject;
cppobject.draw();
}
You should use opaque pointers and only include C++ headers in the file that implements your Objective-C class. That way you don't force other files that include the header to use Objective-C++:
// header:
#import <UIKit/UIKit.h>
#import "GLView.h"
struct Opaque;
#interface GLViewController : UIViewController <GLViewDelegate>
{
struct Opaque* opaque;
}
// ...
#end
// source file:
#import "myCppFile.h"
struct Opaque {
myCppFile cppobject;
};
#implementation GLViewController
// ... create opaque member on initialization
- (void)foo
{
opaque->cppobject.doSomething();
}
#end
Make sure that all files that include GLViewController.h are Objective-C++ sources (*.mm).
When you include C++ code in the header of your view controller, all sources that import this header must be able to understand it, so they must be in Objective-C++
You need to declare the C++ objects in an interface block in your .mm file.
In .mm:
#include "SomeCPPclass.h"
#interface SomeDetailViewController () {
SomeCPPclass* _ipcamera;
}
#property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)blabla;
#end
I think you need to set the following flag to true in your project settings:
GCC_OBJC_CALL_CXX_CDTORS = YES
This should allow you to instantiate C++ objects in your Objective-C classes.