Feel the ease of time tracking with NR Time. Log your time expenses in the billing-friendly format with minimal effort.
Posted: August 6th, 2009 | Filed under: Programming | Tags: iPhone, objectivec | No Comments »
What if you followed all official (first, second, third) and unofficial tutorials to the letter, but still can’t make your tests run? Well, it’s what it was for me today. Here are two things I couldn’t find anywhere and had to figure myself:
- If you see only one Unit Test Bundle build error, and it’s says “Failed tests for architecture ‘i386′ (GC OFF)” (slight variations depending on your platform and the state of the GC flag), you don’t have any tests in the bundle. Yes, go check that counter in the Compile Sources task of your test target.
- If you added the application as the dependency to the test bundle, wrote the test that uses one of the classes and see the link error like below, you didn’t add any source files being tested to your test bundle for compilation. I know it’s insane to add them one by one, but it’s how they designed it. To make it a bit easier, right-click on the “Groups & Files” header, choose “Target Membership”, make sure your tests bundle is you current target and start checking missing sources.
".objc_class_name_XYZ", references from:
literal-pointer@__OBJC@__cls_refs@XYZ in XYZTests.o
sumbol(s) not found
collect2: Ld returned 1 exit status
Posted: August 6th, 2009 | Filed under: Programming, iPhone | Tags: iPhone, uinavigationcontroller, uitabbarcontroller | No Comments »
On iPhone, there’s the tab bar control that we, developers, can use to switch between several pages. It appears as a black stripe with icons at the bottom of the screen. Probably, the most famous is the tab bar of the Clock application:

It can show up to 5 icons and if there’s not enough space left, the last one is replaced with the “More …” item. When clicked, it shows the nice view with the list of all available options that didn’t fit into the tab bar. UITabBarController delegates the job to the UINavigationController. Now why I tell all this?
By default, the UINavigationController uses that standard blue top bar, but what if I want it to be Black as in the rest of my app?
First, let’s change the style of the “More …” page top bar. It’s all pretty straight forward as you can see. Just get the navigation controller and set the style of its bar:
tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleBlack;
Finally, let’s change the style of the bar on the “More …” / Edit page (that says “Configure” by the way). It appears that you can’t drill down into the object model like above, but have to catch the moment when the editor is about to come out and do the magic. For this to happen, you needthe tab bar delegate (UITabBarControllerDelegate protocol) and use the tabBarController:willBeginCustomizingViewControllers: callback to nail it:
tabBarController.delegate = self;
and later:
- (void)tabBarController:(UITabBarController *)tabBarController
willBeginCustomizingViewControllers:(NSArray *)viewControllers {
UIView *views = [tabBarController.view.subviews objectAtIndex:1];
UINavigationBar *navBar = [[views subviews] objectAtIndex:0];
navBar.barStyle = UIBarStyleBlack;
}
In the above 3-liner, you can see that we do the trick to actually find the navigation bar. It works nicely on iPhone 3.0, but may change in the future, so be careful.
Don’t forget that you can always subclass UITabBarController and hide all this customization logic inside to stay on the clean design side. In my work, I do that and used the tabBarController here to clearly state what model we operate on.
That’s all, folks. Hope it helped someone to save a couple of hours.
Posted: August 2nd, 2009 | Filed under: Programming | Tags: bash, textmate, Tips | No Comments »
There’s a couple of Bash patterns I often use in my scripting that I would love to share. Both of them are immediately useful in your TextMate practice through commands, and that’s what makes the study really fun.
Default Values
Assuming that you want a user to give you the path as the first argument and fall back to something if he didn’t, normally you may have done this:
if [ -z $1 ]
then
MY_PATH=/usr/local
else
MY_PATH=$1
fi
And here’s how you do it geeky Bash-style:
DEFAULT_PATH=/usr/local
MY_PATH=${1:-$DEFAULT_PATH}
Shorter and delivers the message way more transparently, doesn’t it? That minus sign before the $DEFAULT_PATH is critical, so don’t miss it. Which leads us to the next tip.
String Splitting
If you need only some part of the string, you can easily cut it out knowing the start/end indexes or just one of them.
VALUE=abcd
INDEX=2
# Gives 'ab'
LEFT_FROM_INDEX=${VALUE:0:INDEX}
# Gives 'cd'
RIGHT_FROM_INDEX=${VALUE:INDEX}
What about TextMate?
Now that you possess the secret knowledge, you can use it in your TextMate commands. One example scenario mentioned all around is replicating the common pattern where the command operates either on the selection or the current word. Here’s how it would look:
${TM_SELECTED_TEXT:-$TM_CURRENT_WORD}
Note the order of the parameters, TM_CURRENT_WORD is the default, not the other way around. If there’s nothing selected, it will come in handy. Now, if you read my previous posts on Ruby / Shell code execution, this should ring a bell. Here’s how the selection-or-line logic would look:
${TM_SELECTED_TEXT:-$TM_CURRENT_LINE}
Amazingly simple and ultimately useful!
Posted: August 1st, 2009 | Filed under: Programming | Tags: cgi, ruby | No Comments »
I’m a big fan of Ruby, and still every time I need some simple server-side functionality — you know those go there, take that sort of tasks — I have to revert to good old PHP. It’s obvious that building a full-blown Rails app is an overkill.
This morning I need another tiny little script to parse the HTML page and get some info back in JSON. And all this for a static web-site, so that it can’t be hosted locally. So what do I do? Getting back to roots…
Many web servers let you run CGI scripts, which is nothing else but an ordinary script that is executed by an interpreter of your choice and the results are thrown back as the response. Of course, it’s a bit more complicated that that, but the gist is accurate, and what’s more important is it’s hilariously easy to write.
If you happen to have a site with CGI enabled, you can try it right away. If not, well, check the hosting docs, or your web server configuration to see if there’s anything you can do. Most often, you will find the cgi-bin directory where all magic happens sitting next to public_html or www in your web site location root. Don’t forget that everything you put into cgi-bin has to be executable by the web server. It means that either you need to give it the correct ownership and the owner execution permissions or simply let everyone run it (which is dangerous sometimes, but good enough for a couple of tests).
So, here’s what you can try throwing into /cgi-bin/ruby.cgi:
#!/usr/bin/ruby
puts 'Content-type: text/plain'
puts
puts 'Test Complete'
Now when you run it by entering http://your_site/cgi-bin/ruby.cgi in the address line, you will get the nice and shiny “Test Complete” string.
Note that content type declaration and the empty line after it. Two things are important about these:
- Both lines are mandatory
- You can set any content type you like, not just output things in plain text. It can be JSON, XML, HTML or GIF, PNG, PDF — anything you can think of.
Now why don’t you throw a couple of gems into the mix and build something cool, fast and breathing!
Posted: August 1st, 2009 | Filed under: Programming | Tags: ruby, textmate | 4 Comments »
In one of my previous posts I mentioned how nice it is to execute Ruby code and get the results in comments. This is quite useful when you build documentation or quickly test some ideas.
There’s one other method of doing this which is based on the Textmate’s ability to execute the code line at cursor or the selected piece of text. It’s as easy as it sounds.
- First, enter the piece of Ruby code to execute, having in mind that the result of the last line will be output below:
- Now select all three lines and press ⌃⇧E (Execute line as Ruby). The result will appear on the next line
One neat trick is that you can use printf(…) function to format your output and, in this case, the result of the last line won’t be displayed, but the printed output will.
I find it useful when there’s something to try right inside the document or there’s some formula to solve. It lets me stay where I am and not to open a separate document just for the Ruby snippet.
As a little bonus, I just want to mention that you can run unix commands in the same fashion and get the output right in your document. For example:
- Enter the following line and leave the cursor at the end of it:
- Press ⌃R (Text / Execute Line Inserting Result) to execute it. The result will be placed below.
The list of all nginx processes will be dumped. Sweet, ha.
Posted: July 29th, 2009 | Filed under: Programming | Tags: config, ssh | No Comments »
Many people around me don’t know about the magical ~/.ssh/config file. If you are among of them, just read on and pretend you knew it all the way.
Here’s a simple, yet useful sample that shows just the two most essential features that you can start using today:
Host someplace.com
Port 22222
Host 174.10.20.30
IdentityFile ~/.ssh/id_rsa.office
What do we see here?
- ~/.ssh/config is a plain-text file. Note that it’s file permissions should be “600″ (read/write by owner only), and ~/.ssh directory permissions must be “700″. It is important. If they are too permissive, SSH won’t trust your configuration.
- The file consists of sections that start with the “Host” declaration. Each section corresponds to one host specified either by name or IP address.
- Statements in the sections are indented (traditionally with 3 spaces).
- “Port” statement suggests the port number to use when connecting to the given Host.
- “IdentityFile” statement lets you use a non-default (id_rsa) private key. I see you burst into tears. Yes, you can have many keys for different occasions — personal and team; any number of them.
These are just few options that I find the most useful in my everyday practice, but feel free to explore it deeper.
One important thing I need to mention is that this configuration applies to absolutely all cases when you open the SSH connection by standard means, not just when SSH’ing somewhere directly. You can do SCP to transfer files, access remote repositories with SVN and GIT, deploy your developments with Capistrano and much more.
Have fun!
Posted: July 28th, 2009 | Filed under: Programming | Tags: irb, ruby, textmate | 3 Comments »
In my day-to-day development I keep IRB handy just to sketch some statement or check how some method works with a given data set. It’s all quick and helpful, but working in TextMate, I always forget about it also has a tiny small feature that lets us do the same in much more comfortable fashion. The next time I need to check something, I will:
- Open an empty text document
- Set type to Ruby with ⌃⌥⇧R
- Enter my ruby code just like this:
a = [1, 2, 3]
b = [2, 3]
a + b # =>
a - b # =>
- Finally, run it with ⌃⌘⇧E and get this as the result:
a = [1, 2, 3]
b = [2, 3]
a + b # => [1, 2, 3, 2, 3]
a - b # => [1]
See? Handy.
By the way, I may have noticed that this format is frequently used in Ruby docs. Now you know how do they do it, right?
Posted: July 27th, 2009 | Filed under: Programming | Tags: gems, plugins, rails | No Comments »
To keep up with the quickly evolving Rails community I constantly read many blogs and watch podcasts. Here are some curious new gems I discovered recently.
Rack::Bug. A comprehensive debugging plugin for your Rails applications that sits on the Rack middleware level and captures a ton of curious info ranging from request parameters and Rails environment to the request processing timings, SQL execution details and much more. It’s not suitable for the older Rails-based projects as it needs the Rack which has become the part of Rails (or Rails was adapted to run in the Rack stack to be precise) just recently, but for new projects it can be a life saver.
FiveRuns TuneUp. While still in beta, this plugin and their server-side service may give a good quick insight into what’s going on under the hood of the request processing. It sits at the top of the page as a bar and gives a nice JavaScript popup with the response rendering tree and model schemas. I noticed a some quirks and wasn’t able to dig deep enough to nail a couple of problems I noticed, but it’s a great general-purpose tool to quickly see who’s the greediest of them all.
NewRelic RPM. Not new, but for the completeness sake. A great paid (with a free plan) service with the app-side agent plugin for performance data collection. Collects ALL possible data about your app down to the execution plans of your statements and error trace-backs, but costs a fortune. None of my clients went higher than a Bronze plan because of their cosmic prices. I’m sure they have their customers, it’s just not for everyone. Still, thanks for NewRelic guys (honest thanks), there’s a way to get access to all info if you have a head, hands and some time.
Metric_fu. All-in-one code analysis package grouping several other tools and providing a unified web interface with graphs and other niceties. Not much more to say here. Useful as hell if you on the quality side of the application development business.
Whenever. Quoting the author, “Whenever is a Ruby gem that provides a clear syntax for defining cron jobs”. This is one of an awesome piece of software. Rare Rails app these days needs no background / periodical processing. Even simple news e-mail delivery is done with cron + rake or similar solutions. This tool lets you keep your cron jobs definitions in ruby and with a tiny bit of an effort, you can add the crontab regeneration to your deployment process. No need to handle all these crontabs manually any more.
That’s all folks. However, this brings up an interesting question. How do you discover what new tools / gems emerge in the Rails realm these days? It develops insanely rapidly.
Posted: July 27th, 2009 | Filed under: Programming | Tags: bundles, textmate | 2 Comments »
Reviewed and updated the list of TextMate bundles that I use today. Figured that it’s good idea to hold them all in one place “~/Library/Application Support/TextMate/Bundles” and use Git to synchronize them with Github repositories. This way, everything is in one place and is easily manageable. I also think it’ll be nice to have a tiny little script to update them all every now and then in the background automatically.
Here’s the list of the bundles that I use today. Some actively and some not:
- CSS — git://github.com/textmate/css.tmbundle.git
- Capistrano — git://github.com/vigetlabs/capistrano-tmbundle.git
- Git — git://gitorious.org/git-tmbundle/mainline.git
- HTML — git://github.com/textmate/html.tmbundle.git
- JSON — git://github.com/textmate/json.tmbundle.git
- JavaScript — git://github.com/textmate/javascript.tmbundle.git
- JavaScript JQuery — git://github.com/textmate/javascript-jquery.tmbundle.git
- JavaScript Prototype & Script.aculo.us — git://github.com/textmate/javascript-prototype-and-script.aculo.us.tmbundle.git
- Markdown — git://github.com/textmate/markdown.tmbundle.git
- RSpec — git://github.com/dchelimsky/rspec-tmbundle.git
- Ruby — git://github.com/textmate/ruby.tmbundle.git
- Ruby Haml — git://github.com/douglasjarquin/ruby-haml-tmbundle.git
- Ruby Mocha — git://github.com/joshuabates/ruby-mocha.tmbundle.git
- Ruby On Rails — git://github.com/textmate/ruby-on-rails.tmbundle.git
- Ruby Sass — git://github.com/aussiegeek/ruby-sass-tmbundle.git
- Ruby Shoulda — git://github.com/drnic/ruby-shoulda-tmbundle.git
- SQL — git://github.com/textmate/sql.tmbundle.git
- Source — git://github.com/textmate/source.tmbundle.git
- Subversion — git://github.com/textmate/subversion.tmbundle.git
- Text — git://github.com/textmate/text.tmbundle.git
- Textile — git://github.com/textmate/textile.tmbundle.git
Hope this list does any good to someone. If nothing else, it’s my personal reminder of all that locations for the next time I reinstall the OS.
Posted: June 1st, 2009 | Filed under: iPhone | Tags: background, cocoa, development, dictionary, iPhone, lookup | 1 Comment »
First of all, for those who don’t follow me on twitter (@spyromus), this year I’m working on my Cocoa / Cocoa Touch skills. Love it immensely so far and especially how things are nicely done in the iPhone department. A sheer pleasure.
During the last few weeks I tried several iPhone dictionaries and surprisingly all of them ( I mean ALL ) are coded in a strange way. The one of the most important parts — word entry — is implemented in a totally unimaginative straightforward way where it either looks up whatever you enter against their huge databases after every new letter or does that periodically. Both versions block the search box every now and then and don’t let you enter your searches quickly.
No finger pointing here certainly, but you guys know who you are. I tried to contact authors and share this bit of knowledge, but it’s either http://localhost/ as the support link or no link at all, so… the least I can do is to share it here. Hope it’ll be of some help.
Here’s the part of a XYZViewController:
- (void)viewDidLoad {
operationQueue = [NSOperationQueue new];
[super viewDidLoad];
}
- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText {
[operationQueue cancelAllOperations];
SearchOperation *op = [[SearchOperation alloc]
initWithText:searchText dictionary:dictionary controller:self];
[operationQueue addOperation:op];
[op release];
}
Where SearchOperation is a subclass of NSOperation and its main method looks something like this:
- (void) main {
[NSThread sleepForTimeInterval: 0.25f];
if (![self isCancelled]) {
// Do your searching magic here ...
[controller performSelectorOnMainThread:@selector(searchResult:)
withObject:...
waitUntilDone:NO];
}
}
The idea is that when the user types a letter, the searchBar:textDidChange: is called, but it doesn’t do the search right away. Instead it creates an instance of the SearchOperation which represents a lengthy dictionary lookup and queues it. Each SearchOperation sleeps 1/4 seconds before it starts doing any work. In fact, before the actual start it checks if it was canceled and does that periodically during the search.
So when it can be canceled? That’s why we have the cancelAllOperations call in the queuing code. If the user types, every next letter results in the cancellation of previous search operations and none of them will complete or even start. Moreover, the user interface will never be blocked and your users can type away freely and only if they stop, the GUI will follow and show the results.
You see? Simple.