Does Swift have a #warning equivalent? It's simply used to show a warning in Xcode's own GUI
I'm also interested in whether there's a #error equivalent.
Apple has said #pragma mark is coming soon, it could possibly be the same with this.
Edit
As of Swift 4.2, language level support is available for both build warnings and errors.
#warning("Warning description")
#error("Throws a build error")
Original Answer
Quick, dirty, and oh so elegantly simple all at the same time.
// Description of what you need to fix
var FIX_ME__🛠🛠🛠: AnyObject
Throws a warning that 'FIX_ME__🛠🛠🛠' was never used.
You can add emoticons to the variable name if you like... I often use 😱 and 🛠, for something that really needs fixing I'd even consider 💩. You can replace FIX_ME__
with whatever you want: ALGORITHM_NEEDS_REVIEW
, BugID_148
, or JOHNNY_YOU_BROKE_THIS
are some examples.
Quick, no setup, concise, and emoticons can even add humour/personality to your code. Sometimes the most simple solution is the best solution.
In the future, Apple devs may very well release a //WARNING:
landmark, or provide the functionality for another named landmark.
To envoke this functionality with Swift in Xcode today however, you could do the following as outlined by Ben Dodson & Jeffrey Sambells:
Add a new Run Script to your target's build phases tab (project settings > build phases > '+' > new run script phase), and paste the following code in the empty box:
TAGS="TODO:|FIXME:"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"
This will force Xcode to flag a warning at compile time for any // TODO:
or // FIXME:
comments you markup.
Alternatively, you could amend TAGS with a custom tag: TAGS="WARNING:"
in the above code which would keep the default behaviour for TODO & FIXME and would raise a compile time warning on any comments marked-up as // WARNING:
.
http://bendodson.com/weblog/2014/10/02/showing-todo-as-warning-in-swift-xcode-project/ http://jeffreysambells.com/2013/01/31/generate-xcode-warnings-from-todo-comments
EDIT: 18/11/14
@david-h raised a good point in his comment. If you wanted to only raise these warnings in a specific build configuration, you could do the following:
if [ "${CONFIGURATION}" = "Debug" ]; then
TAGS="TODO:|FIXME:"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"
fi
Alternatively, you could use "Release" rather than "Debug" to target production builds only.
Starting with Xcode 10 and Swift 4.2 you will now be able to use #warning
again like so:
#warning("TODO: Clean up this code after testing")
This will show up as a warning in Xcode just as expected!
This works even in combination with #if
checks, for example the following will only show a warning if your target platform is iOS:
#if os(iOS)
#warning("this code is untested in iOS")
#endif
There's also #error
if you want your build to fail.
In Swift using XCode 6 you can use different kinds of landmarks for different purposes. Here's what Apple says about it:
Xcode now supports //MARK:, //TODO: and //FIXME: landmarks to annotate your code and lists them in the jump bar.
So for setting a warning with a description you would use something like this:
//TODO: Clean up this code after testing
If you just want to set a short mark (assuming you will remember what to do), use this:
//FIXME
EDIT: These landmarks however only appear in the jump bar of XCode which might not be what you wish for and what one would expect – especially from the //TODO: and //FIXME marks. I've filed a radar on that: #17776817. Hopefully Apple will add this in the coming builds in XCode 6.
SOLUTION (EDIT 2):
If you install the Swift Linter via Homebrew (run brew install swiftlint
after a brew update
) and add the suggested build script to your project, then you will see all your TODO
and FIXME
landmarks appear as warnings within Xcode. SwiftLint will even add some more warnings/errors that you can configure to fit your needs – I can only recommend using SwiftLint and it solves this problem in a great way!
Still not added by Apple team yet. What I decided to do is probably a cheating, but at least it does show me a FIXME message. So what I do is declare FIXME() function in Swift file:
@availability(iOS, deprecated=1.0, message="I'm not deprecated, please ***FIXME**")
func FIXME()
{
}
and when I call it from any other function it does show a warning, e.g.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
FIXME() // Incomplete method implementation.
return 0
}
For Swift 2 use
@available(iOS, deprecated=1.0, message="I'm not deprecated, please ***FIXME**")
Look at this article.
You can write your own script which will highlight all tags.
TAGS="TODO:|FIXME:"
ERRORTAG="ERROR:"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" -or -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$|($ERRORTAG).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/" | perl -p -e "s/($ERRORTAG)/ error: \$1/"
I proposed and implemented this feature, and it will ship with Swift 4.2. You can use it now by download the master
toolchain on swift.org.
#if os(macOS)
#error("macOS is not supported")
#endif
#warning("finish this")
One CocoaPod that I was using had .swift
in its name, so a directory was returned, which caused the script by Kyle to crash. Adding -type f
to the find
command fixes that problem by only looking at files that match *.swift
instead of also returning directories that match the pattern.
Final code that I used:
TAGS="TODO:|FIXME:"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.swift" \) -type f -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"
If you’re loath to adjust your build setup, another simple home remedy is to stick an editor placeholder in front of the comment:
<#todo#>// stop and fixme!
You get an “Editor placeholder in source file” error when you build, but unlike Jordan’s solution, there’s no live error to annoy you while typing:
After much searching and longing, I'm convinced no such entity exists. I'm still hopeful with the latest Xcode release notes mentioning the continued lack of a #pragma mark mechanism, #warning and #error may also be coming as well.
As an aside, I highly recommend filing a Radar with Apple at bugreport.apple.com to add this functionality (you can dupe 17702491).
We wrote a configurable tool that lets you put warnings and errors in Xcode Issue Navigator based on comment tag and build configuration: https://github.com/doubleencore/XcodeIssueGenerator
Install it:
brew tap doubleencore/tap
brew install xcodeissuegenerator
Then put a line in a Run Script Build Phase:
# Mark WARNINGs, SERIOUSs, and TODOs as warnings in DEBUG builds excluding the Vendor and Third Party directories.
XcodeIssueGenerator -b DEBUG -w "WARNING, SERIOUS, TODO" -x "Vendor/, Third Party/"
Here's an article describing how we use it.
My answer is not quite satisfy your question, but if you want something easy you can use this plugin of alcatraz which works in all the projects without any additional preparations. Just do the following:
1) install Alcatraz (Nice package manager for Xcode Plug-ins) by entering this line in terminal:
curl -fsSL https://raw.githubusercontent.com/supermarin/Alcatraz/deploy/Scripts/install.sh | sh
2) Then restart Xcode and on it launch agree to install all bundles not included in Xcode
3) In Xcode select menu item -> Window -> Package manager -> type in search panel XTodo and press install
4) Relaunch Xcode and again agree to install additional bundles
5) From now press Ctrl + T and you will see all Tags in nice window
It also has preferences for adding new tags
Advantage of this snippet - it doesn't show warnings from Pods:
if [ "${CONFIGURATION}" = "DEBUG" ]; then
TAGS="TODO:|FIXME:|WARNING:|warning:" find "." \( -name "*.swift" \) -not -path "./Pods/*" -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"
fi
How to install:
//WARNING
just like #pragma will be//MARK
but currently neither of them are added to XCode beta. – Lord Zsolt