NSString "预期的 ':"之前']令牌”错误

发布于 2024-12-03 13:30:42 字数 1243 浏览 2 评论 0 原文

我的方法旨在从输入字符串中提取有关游戏级别的信息。输入指定 2D 数组播放区域的大小,以及 2D 数组中哪些点存在哪些项目。

例如,“4,3 . a,b,c . d,e,f . g,h,i . j,k,l”将包含 4 列和 3 行,如下所示(没有连字符):

a---d---g---j

b---e---h---k

c---f---i---l

代码工作正常,直到最后一行,我得到错误: “']' 标记之前应有 ':'”。

我已经尝试解决这个问题有一段时间了,所以如果我错过了一些愚蠢的事情,我会感到非常尴尬!任何帮助将不胜感激。

-(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([levelDataStringColumns 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]];
}

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([levelDataStringColumns 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]];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-12-10 13:30:42

您有一组额外的 []。你想要:

NSString *columnString = [[NSString alloc] initWithString:[levelDataStringColumns objectAtIndex:0]];

You have an extra set of []. You want:

NSString *columnString = [[NSString alloc] initWithString:[levelDataStringColumns objectAtIndex:0]];
因为看清所以看轻 2024-12-10 13:30:42

最后一行有一个额外的括号,将其更改为:

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]];
自我难过 2024-12-10 13:30:42

尝试最后一行:

 NSString *columnString = [[NSString alloc] initWithString:[levelDataStringColumns objectAtIndex:0]];

Try this for the last line:

 NSString *columnString = [[NSString alloc] initWithString:[levelDataStringColumns objectAtIndex:0]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文