Swift: #warning equivalent

Swift

Swift Problem Overview


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.

enter image description here

Swift Solutions


Solution 1 - Swift

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.

Solution 2 - Swift

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.

Solution 3 - Swift

Post WWDC 2018 Update

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.


Pre WWDC 2018 Answer

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!

Solution 4 - Swift

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
}

enter image description here

For Swift 2 use

@available(iOS, deprecated=1.0, message="I'm not deprecated, please ***FIXME**")

Solution 5 - Swift

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/"

This results to: enter image description here

Solution 6 - Swift

As an alternative, if you want something to show up in the warnings panel, you could write something like:

if (false){
   var x = 2;
}

You can't really get any text to show up, but at least it's a more visible marker, especially if you tend to treat (most) warnings like errors.

Solution 7 - Swift

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")

Solution 8 - Swift

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/"

Solution 9 - Swift

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:

editor placeholder

Solution 10 - Swift

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).

Solution 11 - Swift

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.

Solution 12 - Swift

I may be late to the party with Xcode 10 supporting errors and warnings, but simply putting a String:

"Need to finish implementing this"

will produce a warning: String literal is unused with autocompletion still working and the project still compiling.

Solution 13 - Swift

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

enter image description here

Solution 14 - Swift

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:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSomeGuyView Question on Stackoverflow
Solution 1 - SwiftJordan SmithView Answer on Stackoverflow
Solution 2 - SwiftKyle GView Answer on Stackoverflow
Solution 3 - SwiftJeehutView Answer on Stackoverflow
Solution 4 - SwiftinterruptView Answer on Stackoverflow
Solution 5 - SwiftkelinView Answer on Stackoverflow
Solution 6 - SwiftArie LitovskyView Answer on Stackoverflow
Solution 7 - SwiftHarlan HaskinsView Answer on Stackoverflow
Solution 8 - SwiftsflogenView Answer on Stackoverflow
Solution 9 - SwiftMinh Nguyį»…nView Answer on Stackoverflow
Solution 10 - Swiftrcw3View Answer on Stackoverflow
Solution 11 - SwiftsoolwanView Answer on Stackoverflow
Solution 12 - SwiftPrzemysław WrzesińskiView Answer on Stackoverflow
Solution 13 - SwiftNikolay ShubenkovView Answer on Stackoverflow
Solution 14 - SwiftIgorView Answer on Stackoverflow