问题
I am getting this error message and matched my brackets and couldn't find anything wrong. Can you find what's wrong? I really need help. Please. The error I am getting is this "Missing '[' at start of message send expression" I have commented where it occurs down near the end of my code. Please help me.
Thank you.
@interface HomeViewOne ()
@end
@implementation HomeViewOne
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)switchScreenHome:(id)sender {
//Code to switch screen from main menu to game
ViewController *view [[ViewController alloc] initWithNibName:nil bundle:nil]; //My error is on this line, it says Missing '[' at start of message send expression
[self presentModalViewController:view animated:YES];
}
@end
回答1:
The code is missing something useful, like an =
.
Thus the [..]
is not parsed in a meaningful fashion (i.e. as an expression used in an assignment) and results in the given syntax error.
The code is actually parsed as an variable array declaration, consider
X* x[..];
where ..
is
[ViewController alloc] initWithNibName:nil bundle:nil
which results in "Missing '[' at start of message send expression", just as if that code appeared in a statement by itself. Adding another pair of []
's would "fix" that, and actually declare a variable-length array. However, you definetly don't want a VLA here.. just add the assignment operator already.
回答2:
That line should be:
ViewController *view = [[ViewController alloc] initWithNibName:nil bundle:nil]; //My error is on this line, it says Missing '[' at start of message send expression
you need to add a "=
".
回答3:
The line with the error should actually be:
ViewController *view = [[ViewController alloc] initWithNibName:nil bundle:nil];
Note the '=' character
来源:https://stackoverflow.com/questions/22737673/missing-at-start-of-message-send-expression