Johnathon Sullinger
Johnathon Sullinger

Reputation: 7414

Using the // TODO comment with the Xcode IDE

Can some one tell me if this is how the IDE is supposed to work or if I am not understanding how the // TODO commenting feature works. When I place a // TODO, Xcode adds a TODO section in the jump bar. Multiple TODO's places multiple sections with the TODO comment as the section title.

The issue that I am seeing, is that any method that comes after my TODO comment is included as part of the TODO section in the jump bar. Why does is Xcode just automatically adding all of the methods after my comment as part of the TODO ?

Perhaps I am missing the point of why it does this, or maybe I am doing this wrong. Could someone provide some clarification on this for me?

Thanks!

Example

Upvotes: 1

Views: 1198

Answers (1)

Paul B
Paul B

Reputation: 5115

Special comment marks

Not sure if there is a // TODO special comment mark. There are TODO: , FIXME: , MARK: , MARK: - (to put separator). MARK: - Some Text will put a some text with separator. Also there are // ???: and // !!!: - they produce marks as well - just try them (they might not work in Swift).

// TODO without : does not create any marks (as of Xcode 10).

Highlighting comment marks by turning them into Xcode warnings

You can use certain types of comments to produce warnings at build time. Select Project -> Build Phases, press '+' button to add another phase. Choose Run Script on creation. Add as a body of script (make sure Shell is /bin/sh):

KEYWORDS="TODO:|FIXME:|\?\?\?:|\!\!\!:"
find "${SRCROOT}" \( -name "*.swift" \) -print0 | \
xargs -0 egrep --with-filename --line-number --only-matching "($KEYWORDS).*\$" | \
perl -p -e "s/($KEYWORDS)/ warning: \$1/"

Xcode project settings for Build Phases

Now when you build, you'll get warnings with the text of comments.

Custom warnings generated from comments

You are free to limit KEYWORDS to fixes and !!! only. To get these warnings upfront (and not wait for the actual build) just move the newly created Run Script section to the top. Some variations of this solutions can be found here and here.

Throwing custom compile time warnings and errors

In case you need to explicitly rise a warning for some piece of your code Swift allows for the following compile directives:

#if os(iOS)
    #warning("this code is untested in iOS")
#endif

Or

#error("Throws a build error")

Upvotes: 5

Related Questions