unknown type name in objective c - objective-c

I'm pretty new to objective c, and having some basic problems.
I wrote a simple program using a navigator, and everything worked fine.
then I added few lines of code (can't even remember what exactly, and it seems to have no connection to the problem) and the problem occurred. I tried ctrl+z, and the problem remained:
I run the program and get these errors:
1. unknown type name "mainController"
2. property with 'retain (or strong)' attribute must be of object type
while mainController is the first screen to be loaded.
This is the appDelegate.h file:
#import <UIKit/UIKit.h>
#import "mainController.h"
#import "WishesList.h"
#import "Wish.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) IBOutlet UINavigationController *navController;
#property (strong, nonatomic) IBOutlet mainController *viewController; // this line creates the errors
#property (strong, nonatomic) WishesList *WishesArray;
#property (strong, nonatomic) NSIndexPath *temp;
#end
this is the relevant part of the appDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
WishesArray = [[WishesList alloc]init];
temp = nil;
[self setViewController:[[mainController alloc]init]];
[self setNavController:[[UINavigationController alloc]initWithRootViewController:self.viewController]];
[self.window setRootViewController:navController];
[self.window makeKeyAndVisible];
return YES;
}
And this is mainController.h:
#import <UIKit/UIKit.h>
#import "addWishController.h"
#import "displayWish.h"
#import "WishesList.h"
#import "Wish.h"
#interface mainController : UIViewController
#property (nonatomic, weak) WishesList *list;
#property (nonatomic, strong) IBOutlet UITableView *wishTable;
- (void)addWish;
#end
it already worked...
can you figure it out?
thanks

I figured out, that the same error appears if you have an import-cycle:
Class_A.h: #import "Class_B.h"
Class_B.h: #import "Class_A.h"
To fix: look for any imports of the offending class (the error tab is your friend, expand the relevant error for a list of imports). Remove #import's accordingly

This problem happen to me once.
I was importing the "APPDelegate.h" in my h file and in my APPDelegate.h I was importing the file too (it shouldn't be a problem but...)
What I did: I changed the Import from my own .h to .m and it worked :)

As others have mentioned, this is indeed caused by cyclic imports. To fix this remove the imports in one of the classes. But sometimes this is not sufficient. If the classes depend on each other, simply forward-declare the one class in the other:
Class A:
#import <UIKit/UIKit.h>
#class B; //<- this is essential here
#interface A: NSObject
#property(nonatomic, strong) B *b;
//...
In class B we have:
#import "A.h"
#interface B: NSObject
#property(nonatomic, strong) A *a;

#JustAStranger and #NathanielSymer, both are correct!
Anyway, worth remember that this case, below, has the same problem too:
Class_A.h: #import "Class_B.h"
Class_B.h: #import "Class_C.h"
Class_C.h: #import "Class_A.h"
This problem reveal to us how important is take care about owners at our Class relationships. Is very easy creates cycle problems using ObjC headers.

Check the target and the files it is compiling. Perhaps mainController has some how been removed from that target. If so, when building, you would get the message that it cannot be found.

This problem looks like a typo because class names usually start with an uppercase character. Therefore, mainController could/should be MainController. Check the class name to see if the error is indeed a typo, because the compiler is telling you it cannot find any class called mainController.

Related

How to view data from NSTextField Correctly?

Hi i've recently completed a tutorial book on the basics of objective c. And now I'm "Attempting" to make a simple application. Right now I seem to be having the simplest issues which i cannot seem to fix, maybe you could tell me what i'm doing incorrectly.
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
- (IBAction)saveData:(id)sender;
//Below is a simple IBOutlet which I try to retrieve data from when the IBAction saveData occurs
#property (weak) IBOutlet NSTextField *foodName;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
//Here is the saveData IBAction which should print out the value of the NSTextField if the user entered a value (if not returns null)
- (IBAction)saveData:(id)sender {
NSLog(#"%#", foodName.stringValue);
NSLog(#"Saved");
}
#end
The problem I seem to have is the build will fail and give me an error message on this line in AppDelegate.m:
NSLog(#"%#", foodName.stringValue);
the error message is: Use of undeclared identifier 'foodName'; did you mean '_foodName'?
Could someone please explain whats going on and how I can overcome this?
To address the getter method, use self.:
NSLog(#"%#", self.foodName.stringValue);
Also, a minor point: group all your #property declarations together at the start of the #interface statement, but after any instance variable declarations:
#interface MyClass : NSObject {
NSString *_str1;
int _i1;
}
#property (weak) IBOutlet NSString *something;
#property (strong, readonly) NSNumber *somethingElse;
- (int)aMethod:(NSString *)string;
- (void)anotherMethod;
#end

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.

objective-c 2.0 Duplicate interface definition for class when defining private methods

i try to define simple private methods and properties in class but it gives me:
Duplicate interface definition for class 'FBViewController'
this is the .m file :
#import "FBAppDelegate.h"
#import "FBViewController.h"
#import<FacebookSDK/FacebookSDK.h>
#import "FBLoginViewController.h"
#interface FBAppDelegate()
#property (strong,nonatomic) UINavigationController *navController;
#property (strong, nonatomic) FBViewController *mainController;
-(void) showLoginView;
#end
#implementation FBAppDelegate
#synthesize navController = _navController,
mainController =_mainController;
...
...
#end
and this is the *.h file:
#import <UIKit/UIKit.h>
#interface FBAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
UPDATE
added the import files
*SOLVED *
in FacebookSDK there is FBViewController.h already
The error you are getting is
Duplicate interface definition for class 'FBViewController'
And you are looking to class called FBAppDelegate. Try reading the error message again, it may help.

Cannot find protocol declaration for "XXXX"

I am stuck with this problem. I have the following .h file:
#import <UIKit/UIKit.h>
#protocol MapSettingsViewDelegate
- (void)settingsDidUpdate:(BOOL)scheme;
#end
#interface MapSettingsViewController : UIViewController
#property (nonatomic, assign) id <MapSettingsViewDelegate> delegate;
#property (nonatomic, strong) IBOutlet UINavigationBar *navBar;
#property (nonatomic, strong) IBOutlet UINavigationItem *titleItem;
#property (nonatomic, strong) IBOutlet UITableView *TableView;
- (id)init;
- (IBAction)saveAction:(id)sender;
#end
When I declare the following:
#interface MapViewController : UIViewController <MapSettingsViewDelegate>
The compiler complains with the following message:
Cannot find protocol declaration for 'MapSettingsViewDelegate'
I have the same kind of delegate declaration in other files in the same project that are compiled without a glitch. I spent the last four hours trying to figuring out what I am doing wrong. Cleaning the project does not nothing.
Problem solved. As suggested in this answer, I created a new class with a different name and copied all coded from the previous class. Worked flawlessly. It seems XCode lost track of something.
You are running into preprocessor problems. Both of your classes import each other. I think you can solve this by adding #class below your protocol declaration as shown below; and moving your #import "mapViewController.h" line into MapSettingsViewController.m file. The #class tells the compiler that you will include the class content somewhere else (in the .m file).
Note that, on the top of your MapViewController.h class file you can include #import "MapSettingsViewController.h" just like normal. Hope this help. I ran into the same problem myself earlier.
#import <UIKit/UIKit.h>
#protocol MapSettingsViewDelegate
- (void)settingsDidUpdate:(BOOL)scheme;
#end
#class MapViewController
#interface MapSettingsViewController : UIViewController
#property (nonatomic, assign) id <MapSettingsViewDelegate> delegate;
...
...
Import the MapSettingsViewController.h file

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