有没有办法获得 Objective-C 的额外警告(类似 perl 的运行时)?
我最近一直在用 perl 进行一些编程,我发现当您尝试使用未初始化的值进行比较/打印/正则表达式等时,它会抛出一个非常有用的警告 - “使用未初始化的值”然而,(以及许多我一直不想做的其他事情)这让我想知道是否有某种扩展或其他东西可以在 Objective-C 中给我一些额外有用的警告?
我知道像 swizzling 这样的东西可能会生成一些运行时警告,也许通过一些聪明的代码魔法,你甚至可以找到一种方法让 Nil 时不时地对你大喊大叫,尽管这很困难,因为 nil 指针需要评估为 false。
走向极端,我什至认为(不是在这里开始辩论)当我在决策结构末尾没有“else”语句时能够收到警告,那就太好了。
因此,把这个问题分成两部分,即这有多少是可能的,以及有多少是存在的?
I've been doing some programming in perl lately, and I found that it throws a really useful warning - "Use of uninitialized value" when you try to do a compare / print / regex etc with a value that you somehow didn't initialize yet, (along with a host of other things I always didn't mean to do) and it got me wondering if there is some kind of extension or something that can give me some extra useful warnings in Objective-C?
I know with things like swizzling it would be possible to generate some runtime warnings, maybe with some clever code magic you could find a way to make Nil yell at you from time to time even, although that's tough because nil pointers need to evaluate false.
Taking it to an extreme I even think (not to start a debate here) it would be great if I could get a warning when I don't have an "else" statement at the end of a decision structure.
So to put this in a bit of a two-part question, how much of this is possible, and how much of it exists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这取决于编译器。假设您使用的是 GCC,您可以使用大量警告选项可以启用。使用 -Wall -Wextra -pedantic 选项进行编译是一个不错的选择 - 如果您的代码使用这些选项编译时没有警告,那么这是一个很好的开始(尽管它当然不能保证代码没有缺陷) 。
如果您使用 Xcode 作为 GCC 的前端,那么您可以通过项目设置启用这些警告选项。
Yes, it depends on the compiler. Assuming you're using GCC, there's a host of warning options that you can enable. Compiling with the
-Wall -Wextra -pedantic
options is a good choice—if your code compiles warning-free with those options, that's a great start (though of course it does not guarantee defect-free code).If you're using Xcode as a frontend to GCC, then you can enable those warning options via the project settings.