Swift: #warning equivalent

前端 未结 14 1612
無奈伤痛
無奈伤痛 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: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.

提交回复
热议问题