Unknown Type Name in Xcode (even with #class declaration) - objective-c

I'm having trouble with a simple app setting up a data controller. I get an error on the line #property (strong, nonatomic) BirdsListDataController *dataController; in BirdsListViewController.h. I've tried my best to use a #class declaration of BirdsListDataController, as well as trying to remove any #import statements from the .h files and tried to remove a circular #import which you can find commented out in the top of BirdsListViewController.h. I'm guessing it's something simple.
BirdsListViewController.h
#import <UIKit/UIKit.h>
#class BirdsListDataController;
#interface BirdsListViewController : UITableViewController <UITextFieldDelegate>
{
// NSMutableArray *listOfBirds;
IBOutlet UITextField *addNewBirdTextField;
}
//#property (nonatomic, retain) NSIndexPath *checkedIndexPath;
#property (nonatomic, retain) NSString *textLabelContents;
#property (nonatomic, retain) NSMutableArray *workingArray;
#property (strong, nonatomic) BirdsListDataController *dataController;
#property (strong, nonatomic) IBOutlet UITableView *birdListTableView;
#end
BirdsListViewController.m
#import "BirdsListViewController.h"
#import "BirdsListDataController.h"
#interface BirdsListViewController ()
#end
#implementation BirdsListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
...
BirdsListDataController.h
#import <Foundation/Foundation.h>
#class BirdName;
#interface BirdsListDataController : NSObject
#property (nonatomic, copy) NSMutableArray *listOfBirds;
-(NSUInteger)countOfList;
-(BirdName *)objectInListAtIndex:(NSUInteger)theIndex;
-(void)addBirdNameWithName:(BirdName *)bName;
#end
BirdsListDataController.m
#import "BirdsListDataController.h"
//#import "BirdsListViewController.h"
#import "Bird.h"
#implementation BirdsListDataController
-(id)init
{...
I'm still really new to iOS and Objective C, so hopefully my code isn't too awful to troubleshoot. Thanks for the help.

For people looking for a better answer than comment/uncomment your code, a better solution is to clean your project and to delete your derived data. Once you've fixed your circular references, the keystroke Command+Shift+K will clean your project, or you can go to and select Product->Clean.
To delete your derived data, open Organizer, click on the Projects tab, navigate to your project in the sidebar. You should see "Derived Data" under the project name header. To the right of that should be a button saying delete. If it is enabled, deleting the derived data can also remove hanging errors.
As way of explanation, it seems sometimes that Xcode becomes out of sync with a project, holding on to errors that no longer exists. This is better in more recent version, but still happens occasionally.

I'm not certain what is causing your problem, but a few things:
In the code that you've presented there is no reason not to import BirdListDataController.h in BirdListViewController.h, since there is no reference to BirdListViewControllers in BirdListDataController.h. So try replacing your #class declaration with an #import statement.
In BirdListDataController.h you declare #class BirdName, but in BirdListDataController.m you import Bird.h instead of BirdName.h. It seems like something could be wrong there, although I would have to see the code for BirdName.h and Bird.h to know for sure.

In my case I had duplicate class names in different folders structure, Once I removed the new class and named it differently everything worked again.
So to translate this into a practical solution, as per "shA.t"'s comment:
if you comment/uncomment your code, or clean project as above
answers suggested but still doesnt solve it:
take a step to look back at recent classes changes and double check
all class names are unique even if in different directories, double
check
them all
if duplicate class name found: make a backup of that
code, delete that class (not just reference, but to trash too)
create a new class with unique name and incorporate the backed up
code
For this particular duplicate class name scenario, this will save you the hassle of importing and commenting your #import "class.h"

Related

xcode objective c missing #end on NSObject

I've been racking my brains for the past 2 hours regarding this issue and I couldn't find any solution through searching or by removing the imported files of my CustomTableCell.
Here's my class (.h)
#import <UIKit/UIKit.h>
#interface MatchTableCell : UITableViewCell{
}
#property (nonatomic, weak) IBOutlet UILabel *matchId;
#property (nonatomic, strong) IBOutlet UILabel *fighter1Name;
#property (nonatomic, weak) IBOutlet UILabel *fighter2Name;
#property (nonatomic, weak) IBOutlet UILabel *status;
#end
This is what's popping up:
Missing '#end' .. this is popping up on the #interface line
Expected Identifier or '(' .. this highlights the first property
Thoughts?
EDIT: Solution. Apparently I can't post this as an answer even though that's how I solved the issue. Anyway, just in case it helps anyone, check the imports of the ViewControllers (or objects) that imports the file getting the error
Well, that was very tedious. Found a ViewController that is imported by a second viewcontroller, this second viewcontroller imports this NSObject. The first viewcontroller, for some reason got changed from UITableViewController to UIViewController. Thanks to #rob-mayoff for the idea
You are missing the #end directive at the end of one of your other .h files. Go through the .h files in your project one by one. For each one that has an #interface or #protocol section, make sure the section is terminated with an #end directive.
Also to mention that start with checking the recently added files. XCODE starts to show this error any where but doesn't indicate which file is missing the combination of #interface and #end. One of the file will have it missing.

Difference in setting up a class?

I may not have worded the question right, but I am not sure if what I am asking makes 100% so here goes:-)
In Xcode you can set a #class (name of class) above the #interface in the header file.
Is this the same as changing the the UIViewController in the name of the class? See code below:
So is this the same -
#class CoreDataClass;
#interface FlipsideViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
}
//This file declares the UITableView
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) CoreDataClass *cdc;
As this:
#interface FlipsideViewController : CoreDataClass <UITableViewDataSource, UITableViewDelegate>
{
}
//This file declares the UITableView
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#end
??
If this is not the same, how is it different and what are advantages to the different implementation?
The Difference is only really asked if they are similar:-)
#class is not used to create a class, but to forward declare another one. See this question for a good explanation.
They are not the same at all. The first case is a 'forward declaration' - you are telling the compiler that the class CoreDataClass exists, so that you can refer to it in your header file without actually importing the files that define it.
The second case, you are declaring that FlipsideViewController is a subclass of CoreDataClass, and inherits all its methods and instance variables.
They're not even related. The difference is that the superclass ("parent" class) of your view controller will be different (and this can lead to nice unrecognized selector errors...). Forward-declaring a class using the #class keyword is just a convenient way of referring to a class when one doesn't want to import a whole framework header hierarch just in order to refer to one class. I. e., if you don't need to know anyting about a class except that it exists, you can use this keyword. Be careful, however, if you maks heavy use of the class - in those cases, the class forward-declaration is not considered a good solution.
In first case when you use #class it's inform XCode that you will be using CoreDataClass somewhere and you will #import header for example in .m file, in second case you're inherit from CoreDataClass (you will get access to all public and protected properties)

trouble with declaring a delegate

i have two views with view1 calling view2. i need to pass data from view2 back to view1. so i am attempting to set up a delegate. here's what i got in view controller 2:
.h file
#protocol addEventDelegate <NSObject>
-(void) setAddedEventFlag:(BOOL) hasAddedEvent;
#end
#interface AddEventViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDelegate>
#property (weak, nonatomic) id delegate; //changed from strong to weak
i then #synthesize delegate in the .m file
when try to include the addEventDelegate for the first view controller, xcode can not find it:
.h file
#import "AddEventViewController.h"
#interface FieldReportViewController : UIViewController <UITextFieldDelegate,
UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDelegate, addEventDelegate>
i get the error: "Cannot find protocol declaration for 'addEventDelegate'".
what is wrong?
EDIT:
//code
ERRORS:
Make sure your spelling is correct.
Make sure that AddEventViewController.h/.m are added to the project.
Other than that, what you have is fine.
Edit
Something else I would suggest is to rename your delegate, perhaps there is a naming conflict. Although I haven't seen any issues with 'add' and 'set', but I've seen issues in the past when prefixing with, say, 'new'.
Also, clean your project, and rebuild and see if that helps.
There could be a situation if you have multiple targets in your project, and possibly only added AddEventViewController.h/m to one of them, and you are building/debugging a different target.
Here's the correct way to define a protocol
#protocol addEventDelegate; // forward declaration for delegate property
#interface AddEventViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDelegate>
{
id <addEventDelegate> *delegate
}
#property (weak, nonatomic) id <addEventDelegate> *delegate;
#end // interface
#protocol addEventDelegate <NSObject>
// #optional // if you want to make it optional
-(void) setAddedEventFlag:(BOOL) hasAddedEvent;
#end // protocol
Solved the issue. i had an #import loop. I was #importing all my classes in my .h files. i changed to #Class in .h file and moved the #import's to the .m files and works like a charm now.
You can import addEventDelegate in FieldReportViewController.m file
#interface FieldReportViewController ()<AddEventDelegate>
#end
This one is working in my app

"Expected a type" error pointing to the return type of a method

I've attempted to compile, but every time I do, one method throws a strange "expected a type" error. I have a method in the header:
-(ANObject *)generateSomethingForSomethingElse:(NSString *)somethingElse;
The error points at the return type for this method. I've imported ANObject into the header using #import "ANObject.h" and ANObject is compiling fine..
Why is this happening?
This is to do with the order that the source files are compiled in. You are already probably aware that you can't call a method before it is defined (see below pseudocode):
var value = someMethod();
function someMethod()
{
...
}
This would cause a compile-time error because someMethod() has not yet been defined. The same is true of classes. Classes are compiled one after the other by the compiler.
So, if you imagine all the classes being put into a giant file before compilation, you might be able to already see the issue. Let's look at the Ship and BoatYard class:
#interface BoatYard : NSObject
#property (nonatomic, retain) Ship* currentShip;
#end
#interface Ship : NSObject
#property (nonatomic, retain) NSString* name;
#property (nonatomic, assign) float weight;
#end
Once again, because the Ship class has not yet been defined, we can't refer to it yet. Solving this particular problem is pretty simple; change the compilation order and compile. I'm sure you're familliar with this screen in XCode:
But are you aware that you can drag the files up and down in the list? This changes the order that the files will be compiled in. Therefore, just move the Ship class above the BoatYard class, and all is good.
But, what if you don't want to do that, or more importantly, what if there is a circular relationship between the two objects? Let's increase the complexity of that object diagram by adding a reference to the current BoatYard that the Ship is in:
#interface BoatYard : NSObject
#property (nonatomic, retain) Ship* currentShip;
#end
#interface Ship : NSObject
#property (nonatomic, retain) BoatYard* currentBoatYard;
#property (nonatomic, retain) NSString* name;
#property (nonatomic, assign) float weight;
#end
Oh dear, now we have a problem. These two can't be compiled side-by-side. We need a way to inform the compiler that the Ship* class really does exist. And this is why the #class keyword is so handy.
To put it in layman's terms, you're saying, "Trust me man, Ship really does exist, and you'll see it really soon". To put it all together:
#class Ship;
#interface BoatYard : NSObject
#property (nonatomic, retain) Ship* currentShip;
#end
#interface Ship : NSObject
#property (nonatomic, retain) BoatYard* currentBoatYard;
#property (nonatomic, retain) NSString* name;
#property (nonatomic, assign) float weight;
#end
Now the compiler knows as it compiles BoatYard, that a Ship class definition will soon appear. Of course, if it doesn't, the compilation will still succeed.
All the #class keyword does however is inform the compiler that the class will soon come along. It is not a replacement for #import. You still must import the header file, or you will not have access to any of the class internals:
#class Ship
-(void) example
{
Ship* newShip = [[Ship alloc] init];
}
This cannot work, and will fail with an error message saying that Ship is a forward declaration. Once you #import "Ship.h", then you will be able to create the instance of the object.
I found this error hapenning when there is circular dependency on the headers. Check if the .h file where you declare this method is imported in ANObject.h
You basically add
#class ANObject;
before #interface!
So, for some reason I was getting this error while trying to set a method with an enum type in the parameters. Like so:
- (void)foo:(MyEnumVariable)enumVariable;
I had previously used it like this and never had an issue but now I did. I checked for circular dependency and could find none. I also checked for typos multiple times and no dice. What ended up solving my issue was to adding 'enum' before I wanted to access the variable. Like so:
- (void)foo:(enum MyEnumVariable)enumVariable;
{
enum MyEnumVariable anotherEnumVariable;
}
Usually when I see an error like this it's because I have a typo on a previous line, such as an extra or missing parenthesis or something.
It may sound stupid, but wrong shelling or wrong use of uppercase/lowercase letterwrong case this.
I got this message, when the variable type was misspelled. See below this below
e.g.
-(void)takeSimulatorSafePhotoWithPopoverFrame:(GCRect)popoverFrame {
instead of.....
-(void)takeSimulatorSafePhotoWithPopoverFrame:(CGRect)popoverFrame {
Strangely enough, changing the order of my imports has fixed this in the past... Try moving the import to the bottom after all your other imports.
I solved it by adding #class class_name to the .h file

The keyword of class and interface at header file in Objective-c [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
#class vs. #import
I am new to Objective-c, I have seen an example look likes:
#import <UIKit/UIKit.h>
#class MapKitSampleViewController;
#interface MapKitSampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MapKitSampleViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet MapKitSampleViewController *viewController;
#end
The above code is stored at "MapKitSampleAppDelegate.h" file, I want to ask what is the meaning of line 3 "#class MapKitSampleViewController;"? Can we change it to #import "MapKitSampleViewController.h"?
I want to ask what is the meaning of line 3
"MapKitSampleViewController"? Can we change it to #import
"MapKitSampleViewController.h"?
Yes.
#class keyword is a "forward declaration". What you are telling the compiler is that this class is going to be used in this class, but the header import for it will be elsewhere.
Most likely if you look in the .m file, you will find that the #import "MapKitSampleViewController.h" will be there.
Why?
The reason why this was implemented (I think, anyway), is to prevent circular imports. Imagine a scenario where the following happens:
Class1.h
#import Class2.h
Class2.h
#import Class1.h
Now, if I'm not wrong, what happens here is that during compilation, it will repeatedly import both header and bad things happen. The #class keyword is meant to prevent this from happening, because the import for those files will happen in the .m files, not in the .h files.
BTW this is a duplicate of #class vs. #import
So you will likely find more in-depth discourse on this topic at that question.
Yes you can change it but this will increase the compilation time and will bring you no benefits.
The "#class MapKitSampleViewController;" is a forward declaration see http://en.wikipedia.org/wiki/Forward_declaration
When using forward declaration you have to take care that you can use the forward declared class name only for type references.