iOS confusing error, "expected ':' before ']' token" help.

falseblackbear

macrumors newbie
Original poster
Apr 6, 2010
17
0
this is the last error in the first part of my code, im just trying to run it and it gives me 2, this one, and one that says it is bailing out because of the last error. im just inserting a simple table, should be easy. here is my code just where the error occurs.

Code:
{
	// create a cell
UITableViewCell *cell = [[UITableViewCell alloc]] //location of error
	initWithStyle:UITableViewCellStyleDefault
	reuseIdentifier:@"cell"
							 
	
	cell.textLabel.texT = [stations objectAtIndex:indexPath.row];					  
	// return it
	return cell;
}
i can always provide my header and more code if needed.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,638
61
Harrogate
You have an extra ] on the line with the error.

Edit to add: and are missing ]; at the end of the init method call.
 

falseblackbear

macrumors newbie
Original poster
Apr 6, 2010
17
0
You have an extra ] on the line with the error.

Edit to add: and are missing ]; at the end of the init method call.
without the extra bracket, i get more errors

can you show me what the code would look like with your corrections, im a total total noob.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,638
61
Harrogate
without the extra bracket, i get more errors

can you show me what the code would look like with your corrections, im a total total noob.
That much is clear!

Let's consider what the code means:
Code:
[object methodname]
tell the compiler that we want to call methodname (or more strictly pass message methodname) to object. Neither object or methodname are optional: without either this is meaningless.
Code:
[UITableViewCell alloc]
tells the class object for UITableViewCell to allocate an instance of that class. It returns an allocated (but un-initialised) instance of that class.

So
Code:
[[UITableViewCell alloc]]
tells the compiler that we want to send a message to that new un-initialised instance but does not tell it the name of the message. Clearly this is a fatal error and the compiler simply has to give up.

As I said in my initial reply you need to remove the second ] (which closes the syntax to make the method call) and move it to the end of the method call. And add a ; to tell the compiler that is the end of that statement.

So
Code:
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
would be valid.

Given what is clearly a fundamental lack of understanding of what you are doing my best suggestion is stop writing code and read The Objective-C Introduction so as you actually know what you are doing instead of randomly guessing and hoping that the compiler can read your mind.
 

MrCrispy

macrumors member
Apr 10, 2008
71
0
Jacksonville, Florida
without the extra bracket, i get more errors

can you show me what the code would look like with your corrections, im a total total noob.
Try this (note, I'm not in front of my mac so I can't test it for sure):

Code:
UITableViewCell *cell = [[UITableViewCell alloc] 
	initWithStyle:UITableViewCellStyleDefault
	reuseIdentifier:@"cell"]; 
//moved the ] to the end of the line to 
//encapsulate all the parameters and added a ;
 

falseblackbear

macrumors newbie
Original poster
Apr 6, 2010
17
0
That much is clear!

Let's consider what the code means:
Code:
[object methodname]
tell the compiler that we want to call methodname (or more strictly pass message methodname) to object. Neither object or methodname are optional: without either this is meaningless.
Code:
[UITableViewCell alloc]
tells the class object for UITableViewCell to allocate an instance of that class. It returns an allocated (but un-initialised) instance of that class.

So
Code:
[[UITableViewCell alloc]]
tells the compiler that we want to send a message to that new un-initialised instance but does not tell it the name of the message. Clearly this is a fatal error and the compiler simply has to give up.

As I said in my initial reply you need to remove the second ] (which closes the syntax to make the method call) and move it to the end of the method call. And add a ; to tell the compiler that is the end of that statement.

So
Code:
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
would be valid.

Given what is clearly a fundamental lack of understanding of what you are doing my best suggestion is stop writing code and read The Objective-C Introduction so as you actually know what you are doing instead of randomly guessing and hoping that the compiler can read your mind.
when i use the code given above, i get 2 brand new errors, is it something with my header?

Code:
UITableViewCell *cell = [[UITableViewCell alloc];  //Expected : b4 ; token
	initWithStyle:UITableViewCellStyleDefault
	;reuseIdentifier:@"cell"] //confused by earlier errors
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,638
61
Harrogate
when i use the code given above, i get 2 brand new errors, is it something with my header?

Code:
UITableViewCell *cell = [[UITableViewCell alloc];  //Expected : b4 ; token
	initWithStyle:UITableViewCellStyleDefault
	;reuseIdentifier:@"cell"] //confused by earlier errors
You have not used the code from above. Or anything even close. You have an extra two ; in your code and one missing at the end of the method call. Once again I suggest that you need to revue the fundamental basics of what you are doing as you clearly don't have a clue.
 

falseblackbear

macrumors newbie
Original poster
Apr 6, 2010
17
0
You have not used the code from above. Or anything even close. You have an extra two ; in your code and one missing at the end of the method call. Once again I suggest that you need to revue the fundamental basics of what you are doing as you clearly don't have a clue.
sorry you are right, i just have one more question, when i insert your (correct) code, the app just crashes upon, opening, do you know where i can find the error responsible, because i dont see any more errors in my code,

there is one warning

Code:
NSString *myfile = [[NSBundle mainBundle]
					    pathForResource:@"stations" ofType:"@plist"];

the warning is "passing argument 2 of 'pathforresouce:eek:fType:' from inconpatible pointer type
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,638
61
Harrogate
sorry you are right, i just have one more question, when i insert your (correct) code, the app just crashes upon, opening, do you know where i can find the error responsible, because i dont see any more errors in my code,

there is one warning
code:
Code:
NSString *myfile = [[NSBundle mainBundle]
					    pathForResource:@"stations" ofType:"@plist"];

the warning is "passing argument 2 of 'pathforresouce:eek:fType:' from inconpatible pointer type
Argument 2 is:
Code:
"@plist"
This is not a valid NSString.

Code:
@"plist"
Software development is about precision. You need to slow down and type more carefully. And read what the compiler is saying: it told you exactly where the error was and what it was.

As to where your code is crashing: use the debugger.