收到错误“格式不是字符串文字且没有格式参数”

发布于 2024-11-30 15:22:24 字数 396 浏览 1 评论 0原文

我在这里看了一下,没有看到我同样的情况。有谁愿意帮忙,谢谢。

我有一个分组表,显示我的足球队即将到来的赛季的比赛。主场比赛和客场比赛。

     NSString *message = [[NSString alloc] initWithFormat:rowValue];
Error Message: Format not a string literal and no format arguments

甚至不确定这来自什么?

我使用了网上找到的教程。复制并粘贴整个内容。我只更改了我的个人表所需的值。这是我得到的唯一错误?有什么帮助吗?

编辑:如果我需要提供更多代码或任何内容,请告诉我!

谢谢你!!

——安东尼·隆巴尔迪

I looked on here and didnt see my same situation. Anyone willing to help, thanks.

I have a Grouped Table that displays my football teams games this upcoming season. Home Games and Away Games.

     NSString *message = [[NSString alloc] initWithFormat:rowValue];
Error Message: Format not a string literal and no format arguments

Not really even sure what this comes from?

I used a tutorial I found online. Copied and pasted the whole thing. I only changed the values I need for my personal table. This is the only error I get? Any help??

Edit: If I need to supply more code or anything, please, let me know!

Thank you!!

-- Anthony Lombardi

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

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

发布评论

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

评论(2

日记撕了你也走了 2024-12-07 15:22:24

不太确定你的意思。我将输入更大的代码块,这可能会有所帮助。

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
                    [self.sortedKeys objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];

NSString *message = [[NSString alloc] initWithFormat:rowValue];

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"You Better Be There Or Be Watching It!"
                      message:message delegate:nil
                      cancelButtonTitle:@"Go Knights!"
                      otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Not quite sure what you mean. I'll input a bigger block of code, that might help.

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
                    [self.sortedKeys objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];

NSString *message = [[NSString alloc] initWithFormat:rowValue];

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"You Better Be There Or Be Watching It!"
                      message:message delegate:nil
                      cancelButtonTitle:@"Go Knights!"
                      otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
思念满溢 2024-12-07 15:22:24

有点老的问题,最初的提问者可能早已不复存在了......但对于历史和未来的读者来说 - 这就是发生的事情。

问题是因为您正在使用 printf 样式格式字符串创建 message - initWithFormat 期望有一个带有 % 字符的格式字符串指示替换 - 例如 %f 表示浮点数、 %d 表示整数等...

您传递的是动态字符串,而不是静态格式字符串(直接在代码中定义的字符串文字)。编译器很困惑,因为你没有传递任何参数。如果您的动态字符串包含 % 替换,地狱之门就会松动,或者在最好的情况下您的应用程序将会崩溃。

由于没有参数的格式字符串没有意义,因此会引发错误。

要修复它,您可以将 rowValuemessage 行替换为:

NSString *rowValue = [listData objectAtIndex:row];
NSString *message = rowValue;

或更简洁:

NSString *message = [listData objectAtIndex:row];

或者使用新的 Objective-C 文字语法和下标:

NSString *message = listData[row];

Somewhat old question, and the original asker is likely long gone... but for history and future readers - here's what's going on.

The problem is because you're creating message with a printf style format string - initWithFormat expects to have a format string with % characters indicating the substitutions - e.g. %f for a float, %d for an integer etc...

Instead of a static format string (a string literal, defined in your code directly) you've passed in a dynamic string. The compiler is confused, because you haven't passed any arguments. If your dynamic string contained % substitutions, the gates of hell would break loose, or at the very best you're apps going to crash.

Since it makes no sense to have a format string with no arguments, it raises the error.

To fix it you can replace the rowValue and message lines with:

NSString *rowValue = [listData objectAtIndex:row];
NSString *message = rowValue;

Or more concisely:

NSString *message = [listData objectAtIndex:row];

Or using new objective-c literals syntax and subscripting:

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