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

Discussion in 'iOS Programming' started by falseblackbear, Apr 6, 2010.

  1. falseblackbear macrumors newbie

    Joined:
    Apr 6, 2010
    #1
    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.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You have an extra ] on the line with the error.

    Edit to add: and are missing ]; at the end of the init method call.
     
  3. falseblackbear thread starter macrumors newbie

    Joined:
    Apr 6, 2010
    #3
    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.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. MrCrispy macrumors member

    MrCrispy

    Joined:
    Apr 10, 2008
    Location:
    Jacksonville, Florida
    #5
    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 ;
    
    
     
  6. falseblackbear thread starter macrumors newbie

    Joined:
    Apr 6, 2010
    #6
    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
    
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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.
     
  8. falseblackbear thread starter macrumors newbie

    Joined:
    Apr 6, 2010
    #8
    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
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    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.
     

Share This Page