I installed TextExpander a few weeks back and have been slowly building up some cool snippets. One thing I often want to do is log something. An object, an int or float, or a point or rectangle. The object and numbers can be pretty easy, unless you are logging the result of some method call or something, but the point and rectangle are prime candidates for snippets. I went back and forth with doing this via Text Macros and doing it via TextExpander. In the end, I like the flow I have with TextExpander. Here they are:
logrect NSLog(@”%clipboard: %%f, %%f, %%f, %%f”, %clipboard.origin.x, %clipboard.origin.y, %clipboard.size.width, %clipboard.size.height);
logpoint NSLog(@”%clipboard: %%f, %%f”, %clipboard.x, %clipboard.y);
logfloat NSLog(@”%clipboard: %%f, “, %clipboard);
logint NSLog(@”%clipboard: %%i, “, %clipboard);
logobj NSLog(@”%clipboard: %%@ “, %clipboard);
As you see, I’m making use of the clipboard. So you select the item you want to log and copy it. Then type the shortcut and you’re done.
For example, say I have a method named myRect which returns a CGRect. So I have some code that says [self myRect] and I want to know what that’s returning. I select [self myRect], copy it, move to the line above or below and type logrect and I get:
NSLog(@”[self myRect]: %f, %f, %f, %f”, [self myRect].origin.x, [self myRect].origin.y, [self myRect].size.width, [self myRect].size.height);
In fact, that’s exactly what I just did while typing the blog post, since TextExpander works pretty much anywhere. 🙂
On the subject of Text Macros, which can be pretty damn powerful in themselves, I refer you to the Pragmatic Programmer site’s Becoming Productive in XCode Screencasts. I’d seen this page before, but didn’t think I wanted to pay for a couple of screencasts showing some shortcuts. Then someone highly recommended it so I took the leap. NO REGRETS! These are great. If you are a beginner or even intermediate XCode user, you will without a doubt find something that saves you $10 worth of your time in there. Some great stuff on Text Macros. Again, the above could have been done with macros, but not sure they could have used the clipboard. You could access the selected text I guess, but then you’d have to either use the menu to select the macro, or assign some shortcut to it. With my snippets, it’s select, copy, type, all keyboard.
Hey, I didn’t know about TextExpander and looks great, could be very useful. Just a side note though, many types in ObjC have NSStringFromXXXX functions. 😉 e.g.
NSLog(@”myRect is %@”, NSStringFromCGRect([self myRect]));