收到错误“格式不是字符串文字且没有格式参数”
我在这里看了一下,没有看到我同样的情况。有谁愿意帮忙,谢谢。
我有一个分组表,显示我的足球队即将到来的赛季的比赛。主场比赛和客场比赛。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太确定你的意思。我将输入更大的代码块,这可能会有所帮助。
Not quite sure what you mean. I'll input a bigger block of code, that might help.
有点老的问题,最初的提问者可能早已不复存在了......但对于历史和未来的读者来说 - 这就是发生的事情。
问题是因为您正在使用 printf 样式格式字符串创建
message
-initWithFormat
期望有一个带有 % 字符的格式字符串指示替换 - 例如 %f 表示浮点数、 %d 表示整数等...您传递的是动态字符串,而不是静态格式字符串(直接在代码中定义的字符串文字)。编译器很困惑,因为你没有传递任何参数。如果您的动态字符串包含 % 替换,地狱之门就会松动,或者在最好的情况下您的应用程序将会崩溃。
由于没有参数的格式字符串没有意义,因此会引发错误。
要修复它,您可以将
rowValue
和message
行替换为:或更简洁:
或者使用新的 Objective-C 文字语法和下标:
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
andmessage
lines with:Or more concisely:
Or using new objective-c literals syntax and subscripting: