Unknown Expected identifier - iphone

I have taken over a project and am fairly new to cocotouch. I was looking throught the code trying to understand things. I didn't think I changed anything but when I went to run It came up with three build errors (it had run just a few minutes before with no problems). The second two seemingly related to the first:
#import <UIKit/UIKit.h>
#protocol MapViewDelegate; //Expected identifier or '(' before 'protocol'
#interface MapView : TTImageView
{
id<MapViewDelegate> mv_delegate; //Cannot find protocol declaration for 'MapViewDelegate'
}
#property (assign) id<MapViewDelegate> mv_delegate; //Cannot find protocol declaration for 'MapViewDelegate'
#end
#protocol MapViewDelegate <NSObject>
- (void)mapView:(MapView *)mv pressedAt:(CGPoint)point;
- (void)mapViewFinishedLoading:(MapView *)mv;
#end
I am using XCode4 which I have just switched to but have had working since the switch.
What is going on here?

MapKit.framework has been added to the project?

Related

Expected identifier or '(' error before #class

I have these 2 classes:
ViewController.h
#import <UIKit/UIKit.h>
#import "UICustomButton.h"
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>{
...
ViewController.m
#import "ViewController.h"
#implementation ViewController
...
UICustomButton.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface UICustomButton : UIButton{
...
UICustomButton.m
#import "UICustomButton.h"
#import "ViewController.h"
#implementation UICustomButton
...
And I get the following errors
1 Expected identifier or '(' - in file UICustomButton.h just before #class
2 Expected ')' - also there
3 Expected a type - when I try to use ViewController in UICustomButton.h
4 in UiCustomButton.m I also get the error
After two hours of googling and trying, I just can't see the problem. Can you please help?
SOLUTION:
Rewriting everything was a good ideea. It made me see that in the file ViewController.m I had accidentally typed a paranthesis '(' on top at the beginning of the file. The compiler didn't poit it out, only UICustomButton.H generated errors.
Don't be afraid to restart/scrap and recreate a class. As you gain more experience correcting complex errors becomes a lot easier.
I'm completely serious about you needing to restart creating this class. Since apparently you can't interpret the message that the compiler is telling you it will take much longer for you to figure out the issue than creating another class, copying the business logic there and deleting the current implementation. Your choice, spend 10 minutes recreating the file or spend another who knows many hours trying to get the syntax Ok.
Even the professionals feel it a strong tool to restart when it's needed. Marshall Huss from Treehouse explains in this podcast why it was preferrable for him too.
PS: and best, you can rename the new class to the old name once you're done.
Looks like you are misspelling ViewController.h. You have Viewcontroller.h in your #import line

iPhone cocos2d "Expected specifier-qualifier-list before ..." and more

I am working on an iPhone game using the cocos2d framework, and recently encountered a problem I can't seem to solve. After adding a new class to my project and doing some work in it, I tried to compile my code and several of the classes I hadn't even touched broke. It seems like those classes just forgot how import external classes. I am getting the error up there in several of my classes, but here is one example:
#import "cocos2d.h"
#import "XZombie.h"
#import "WayPoint.h"
#import "XBuilding.h"
//#class XBuilding;
#interface Hoarde : CCNode
{
float _spawnRate;
int _totalXombies;
NSMutableArray *_path;
XBuilding *_target;
NSMutableArray *_zombies;
bool _zombiesReset;
}
#property (nonatomic) float spawnRate;
#property (nonatomic) int totalXombies;
#property (nonatomic, retain) NSMutableArray *path;
#property (nonatomic, retain) XBuilding *target;
#property (nonatomic, retain) NSMutableArray *zombies;
#property (nonatomic, assign) bool zombiesReset;
- (id) initWithZombieCount:(int)totalXombies Target:(XBuilding *) target SpawnRate:(float)spawnrate;
- (void) resetZombies;
#end
I get the error on the line that reads XBuilding *_target;
If I uncomment the #class XBuilding;, the error goes away, so while that doesn't really solve my problem, it gives me a tool to work around it.
If I do the #class trick for all the files I am having a problem with, I can work around that. The thing is, I get a new - but similar - error besides the specifier-qualifier-list one. Some lines of code give me Expected a ')' before *token or Expected a ';' before *token. Those lines usually gave me the previous error as well, so the #class trick worked as well, but I haven't the slightest idea why this stuff is doing what it's doing. I read somewhere (the cocos2d forums, I think) that renaming the .m files to .mm might do the trick, but it didn't for me.
So, while I can continue working on my project, I would really like to know how on Earth to avoid stuff like that in the future...
Please check whether there is circular dependency, for example, importing ClassB.h in ClassA.h and vice versa. This is the typical scenario where "Expected specifier-qualifier-list before..." error occurs.
I suggest to use only #class in your headers files except:
#importing the super class
#importing the protocols your class implements
The header files should be #imported in your implementation files.
"The Objective-C Programming Language" says:
The #class directive minimizes the amount of code seen by the compiler and linker, and is therefore the simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that may come with importing files that import still other files. For example, if one class declares a statically typed instance variable of another class, and their two interface files import each other, neither class may compile correctly.
More reference:
#class vs. #import
http://www.iphonedevsdk.com/forum/iphone-sdk-development/54417-expected-specifier-qualifier-list-before-xxxappdelegate-error.html

Protocol definition not found

I am having a little trouble with getting a protocol definition to work, and this must be a stupid mistake. I included the header in which the definition is located, but I got the warning, so followed the advice to create a separate header file. I still get the warning that the definition cannot be found (when importing this separate file), and even when I put the definition in the header file of the class using it it gives the warning:
#protocol SubstitutableDetailViewController <NSObject>
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
#end
#interface LauncherViewController :TTViewController<SubstitutableDetailViewController, TTLauncherViewDelegate> {
TTLauncherView *launcherView;
}
So what do I do wrong in my definition of the protocol?
[EDIT: Sorry, there must be an oddity in Xcode, or I am going mad, I did a clean build and now the warning does not come back... but I don't know why]
Put this code in a separate file named SubstitutableDetailViewController.h (I'd prefer SubstitutableDetailViewControllerDelegate.h):
#protocol SubstitutableDetailViewController <NSObject>
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
#end
And then include it in LauncherViewController via #import "SubstitutableDetailViewController.h"

The same error i all headers - error: expected '='... before 'protocol'

I've been working on an iOS project for some time but now I have an error which really confuses me and as long as I can't fix it I can't even compile the project so I need some serious help!
It all began with an:
error: expected '=', ',', ';', 'asm' or 'attribute' before 'protocol'
in PHCluesListViewController.h. The class had not been changed for a long time and what I was working on at the moment of the error had nothing to do with that particular class.
This is what it looks like:
#import <UIKit/UIKit.h>
#protocol PHCluesListViewControllerDelegate;
#class PHClueListTableViewController;
#interface PHCluesListViewController : UIViewController {
IBOutlet PHClueListTableViewController *clueListTableViewController;
id <PHCluesListViewControllerDelegate> delegate;
}
- (void)mapDelegate;
#property (nonatomic, assign) id <PHCluesListViewControllerDelegate> delegate;
#end
#protocol PHCluesListViewControllerDelegate
- (void)mapUp:(PHCluesListViewController *)controller;
#end
There are no syntax errors and there is nothing wrong with the code in it's context either. Later I would learn that if you were to take away all the code from the header file the error would move to a another random header file or if you were to import another header the error would move to this header.
I've tried to restart xcode, to move the project to a new one, to rewrite the code to a new file and to move the project to another computer with another version of xcode - but with no luck.
I am able to run other xcode-projects on my computer.
It seems to me that xcode are trying to compile my headers in this specific project in some unwanted fashion.
Help would be highly appreciated, thanks on before hand!
---------- EDIT!!! ----------
Thanks for the fast respond!! Although I've found the answer.
The error was a "d" written out of bounds of the implementation in a totally different file/class. Found it by chance...
If something similar would happen to anyone else; check for something like a letter written after #end or before #interface/#implementation or anything else that would "cut the edges" of the common syntax.
It's difficult to find the reason for an error like this and I'm a little bit surprised that I found it so quickly among 80 files (only 4.5 hours).
Good luck!!
You're defining #protocol PHCluesListViewControllerDelegate twice. Also, if mapUp is required to be implemented, you might want to add the #required keyword.

My protocol method is not triggered, and no errors while trying

First time working with protocols and it is not working but no errors either...
I have defined and implemented a protocoll in a delegate (BlockPopViewController). Then I try to access it from a UIViewController (BoardViewController) whose view has been added to the delegate as a subview.
The result is that my request to the protocol's method is not creating any errors, but the method is not triggered either. Would be most appreciated if someone has an idea. Thanks in advance!
BlockPopViewController.h
#import "DirectionViewController.h"
#class BoardViewController;
#protocol BVCProtocol
- (void)testing;
#end
#interface BlockPopViewController : UIViewController <BVCProtocol> {}
-(void)testing;
#end
BlockPopViewController.m
#implementation BlockPopViewController
-(void)testing{
NSLog(#"Testing in delegate BlockPopViewController");
}
#end
BoardViewController.h
#class BoardView; //This I cannot import, I think this should be ok instead. Probably cyclic import...
#class Bric; //This I cannot import, I think this should be ok instead. Probably cyclic import...
#protocol BVCProtocol;
#interface BoardViewController : UIViewController {
}
#property(nonatomic, assign) id <BVCProtocol> blockPopViewController;
#end
BoardViewController.m
#import "BlockPopViewController.h"
#import "BoardViewController.h"
#implementation BoardViewController
#synthesize blockPopViewController;
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{
NSLog(#"INSIDE TOUCHESENDED");
[[self blockPopViewController] testing];
}
You are missing a declaration of your BlockPopViewController class which says that BlockPopViewController implements the BVCProtocol protocol.
You should also have your implementation of BVCProtocol derive NSObject in order to get proper memory management behavior. Additionally, you should derive all your protocols from the NSObject protocol in order to prevent compiler warnings when you use NSObject messages on instances of your protocol implementation.
In BlockPopViewController.h you need:
#protocol BVCProtocol <NSObject>
- (void) testing;
#end
#interface BlockPopViewController : UIViewController <BVCProtocol>
#end
In BoardViewController.h you must also #import "BlockPopViewController.h". Otherwise, the compiler won't know anything about the fact that BlockPopViewController is implementing BVCProtocol nor anything about BVCProtocol to begin with.
It would be more logical to declare a protocol in its own .h file and to import that in .h file declaring an implementation of the protocol.
You might find this little tutorial helpful.
Declare protocol outside #interface ... #end context
#protocol BVCProtocol
- (void) testing;
#end
#interface BlockPopViewController : UIViewController <BVCProtocol>
//...
#end
I think the problem I had was that I didn't create an instance of "BlockPopViewController". I changed my solution to not use protocols since it seems like overkill for me. I do have full controll of all involved classes and don't get any benefits using protocols. I do however think that this is what caused it to not work. If there is a reason for using protocols in this kind of situation which I am not aware of, pleas fill me in...
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.