简体   繁体   中英

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.

Looks like you are misspelling ViewController.h. You have Viewcontroller.h in your #import line

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM