Easy to apply tips that can improve your development workflow.
Know available tools and how to use them.
I believe you should use best tool for the job, and there are lots of tools that are perfect for specific tasks.
I wrote about tools I'm using
Write and use code snippets.
We often write similar code, stuff like dispatch to main thread, dispatch after, KVO etc. Use code snippets for that, this can save you lots of keystrokes and time.
Some of my most used ones would be:
- properties declarations
- dispatch_after usage
- init
- KVO
- weakSelf
- Singleton
I've asked others on twitter:
iOS devs: What are your most used code snippets?
— Krzysztof Zabłocki (@merowing_) February 18, 2014
Some of the coolest ones I've seen are created by Kyle Fuller and Mattt
Just be careful not to break DRY principle.
Read clang/gcc documentation to explore some interesting possibilities
Examples:
- Vector extensions and component swizzle, useful when doing lots of math.
typedef float float4 __attribute__((ext_vector_type(4)));
float4 test = {11, 22, 33, 44};
test.xz = 2;
NSLog(@"test %f %f %f %f", test.x, test.y, test.z, test.w);
- C Function overloading to simplify code, instead of calling fabsf, fabsf you could have (Obviously that's more interesting for your functions)
__attribute__((overloadable)) float smart_abs(float x) {
NSLog(@"floats");
return fabsf(x);
}
__attribute__((overloadable)) double smart_abs(double x) {
NSLog(@"doubles");
return fabs(x);
}
- require_super, useful whenever you are creating base classes and want to make sure your subclasses actually remember to call super.
__attribute((objc_requires_super))
- using braces to have implicit return (last references value is returned, same as in ruby). I'm using this in KZPropertyMapper for a neat trick that allows me to execute some arbitrary code and still control returned value
- attribute((constructor)) can be used on C functions for similar behaviour to +(void)load;
__attribute__((constructor))
static void someFunction() {
@autoreleasepool {
//! some code
}
}
Use build phases, scripting and automation for your projects
Warnings
Add this build-phase script AFTER compilation and you will turn all TODO/HACKS etc into warnings (but still be able to use treat warnings as errors)
KEYWORDS="TODO:|VERIFY:|FIXME:|\?\?\?:|\!\!\!:"
find "${SRCROOT}" -name "*.h" -or -name "*.m" -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($KEYWORDS).*\$" | perl -p -e "s/($KEYWORDS)/ warning: \$1/"
Icon versioning
Never again have someone test wrong version of your application.
Build number
You can use either very simple script like this one:
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${INFOPLIST_FILE})
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" ${INFOPLIST_FILE}
or do it properly by using agvtool
Script for graphing dependencies in Cocoa project
Great tool for seeing how tangled is your (or vendor's) code, useful for refactoring or checking if agency that just send you some code knows anything about quality coding.
Use smarter breakpoints
Log all methods that are executed in your app
Avoid temporarly modifying code
Instead you can call NSLog from breakpoints and continue execution.
User space breakpoints
By promoting breakpoints to user space you can have breakpoints that exist only on your machine.
Fast jump to feature you are working on
By combining above 2 ideas you can execute arbitrary code eg. to enable some feature without modifying code, and you can promote it to user space so only you get that behaviour.
Conclusion
There is way more tips we could mention, but let's leave something for future blog posts.