Swift: #warning equivalent

前端 未结 14 1586
無奈伤痛
無奈伤痛 2020-11-28 18:52

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.

相关标签:
14条回答
  • 2020-11-28 18:56

    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__                                                                    
    0 讨论(0)
  • 2020-11-28 18:58

    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")
    
    0 讨论(0)
  • 2020-11-28 18:58

    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/"
    
    0 讨论(0)
  • 2020-11-28 18:59

    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.

    0 讨论(0)
  • 2020-11-28 19:00

    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:

    0 讨论(0)
  • 2020-11-28 19:00

    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:

    0 讨论(0)
提交回复
热议问题