Annotating Swift Code

todo, fixme and compiler directives

Photo by KOBU Agency on Unsplash

Prerequisites:

Terminology

Simple Annotation

The example

func takePicture() -> Bool {
// TODO: Do camera stuff
return true
}
func increment(_ num: Int) -> Int {
// FIXME: Make this increment the input
return num
}
func increment(_ num: Int) -> Int {
// FIXME: Make this increment the input
#warning("Incorrect result returned")

return num
}
func readArr(_ arr: [Int]) {
for i in 0…arr.count {
if i == arr.count {
#error(“err”)
}
}
}

Detect the simulator

@discardableResult
func takePicture() -> Bool {
#if targetEnvironment(simulator)
// TODO: Do camera stuff
return true
#else
return true
#endif
}

Conclusion

Extend your knowledge

Questions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store