// MARK: - What is it?
source link: https://sarunw.com/tips/mark-what-is-it/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
If you have ever created a new view controller, you might have a chance to see this boilerplate code in the newly created view controller.
/* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */
If you are coding long enough, you might already know what it is. But for a newcomer, you might wonder what is // MARK: - Navigation
doing here.
What is this // MARK
MARK
is one of a code annotationthat adds a visual clue to the jump barand minimap.
The following is an example of how // MARK
shows up in the jump bar and minimap.
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { ... // MARK: UISceneSession Lifecycle func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } ... }
The above code will result in a header name "UISceneSession Lifecycle" showing up in jump bar and minimap.
Jump bar Jump bar pop-up menu when clicking on the final segment in the path Minimap (on the right)There are some variations of code annotation, I will briefly go through all of them.
Insert a comment ( //
) with the prefix MARK:
followed by your section heading.
// MARK: View life cycle override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. }MARK in jumpbar MARK in minimap
Mark in Objective-C
You can also use #pragma mark
and #pragma mark -
in Objective-C. Which is equivalent to // MARK:
and // MARK: -
.
#pragma mark View life cycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. }
Add a separator line
Throughout the article, you might notice a MARK
with and without a hyphen ( -
). This hyphen has a special meaning. If you put a hyphen before the comment part of an annotation, it will add a separator above your annotation in three places: code, jump bar, and minimap.
// MARK: - Navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. }Separator showing in code, jump bar, and minimap
There is also a less less-known variation on the separator. If you put a hyphen after a comment, it will add a separator below the comment.
// MARK: Navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. }Separator below a comment
If you want just a separator, that also possible with a hyphen without any comment.
// MARK: - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. }Separator without comment
There are two more annotations that don't have a visual clue on code or the minimap, but convey special meaning and icon in the jump bar.
There is a // TODO:
which will show as a to-do icon in the jump bar.
private func longProcessingTimeFunction() { // TODO: Need optimization }TODO annotation in a jump bar show as a to-do icon
Add a bug fix reminder
To add a bug fix reminder, use FIXME
. Which will add a shiny band-aid icon in the jump bar. This one is quite standout from other annotations, which is good.
private func absolute(_ x: Int) -> Int { // FIXME: Returns incorrect values for negative arguments return x }FIXME annotation in a jump bar show as a band-aid icon
These annotations exist to help you annotate your code. So that you can section and quickly navigation within the file (MARK), pinpoint the remaining tasks (TODO), and remind you for the remaining problems (FIXME).
There is no rule on how to use these, this article shows you all possible ways to annotate your code. How do you use this tool is solely up to you.
Feel free to tweet me your creative way of using these annotations. Right now, I mostly use it for section delegate and data source.
Yes, it also supports emojiRecommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK