NSString "Expected ':' before ']' token" error
My method aims to extract information on a game level from an input string. The input specifies the size of the 2D array playing area, as well as what items are present at what points in the 2D array.
For example, "4,3 . a,b,c . d,e,f . g,h,i . j,k,l" would comprise 4 columns and 3 rows, to look like this (without the hyphens):
a---d---g---j
b---e---h---k
c---f---i---l
The code works fine until the last line, where I get the error: "Expected ':' before ']' token".
I've been trying to solve this for a while, so I'll be quite embarrassed if it's something stupid I've missed! Any help would be much appreciated.
-(void)readLevelDataFromString:(NSString*)inputString {
//remove spaces from the input
NSString *tempString = [inputString stringByReplacingOccurrencesOfString:@" " withString:@""];
//make mutable
NSMutableString *levelDataString = [NSMutableString stringWithString:tempString];
//trim first 4 characters, which we don't need
[levelDataString deleteCharactersInRange:NSMakeRange(0, 4)];
//separate whole string into an array of strings, each of which contains information on the particular column
NSArray *levelDataStringColumns = [levelDataString componentsSeparatedByString:@"."];
NSAssert([levelDataS开发者_开发技巧tringColumns count] == numColumns, @"In the level data string, the number of columns specified did not match the number of X tiles present.");
NSString *columnString = [[NSString alloc] initWithString:[[levelDataStringColumns] objectAtIndex:0]];
}
You have an extra set of []
. You want:
NSString *columnString = [[NSString alloc] initWithString:[levelDataStringColumns objectAtIndex:0]];
You have an extra bracket on the last line, change it to this:
NSString *columnString = [[NSString alloc] initWithString:[levelDataStringColumns objectAtIndex:0]];
Try this for the last line:
NSString *columnString = [[NSString alloc] initWithString:[levelDataStringColumns objectAtIndex:0]];
精彩评论