Computers are like air conditioners - they stop working properly if you open Windows.

—read on a T-Shirt

MultiMarkDown LaTeX Export with Byword

I really love Byword (bywordapp.com) by metaclassy and the MultiMarkDown (MMD) to LaTeX Export feature is simply awesome.

Here’s one thing that really annoys me though:

Java is a high level programming language. It’s unproductive to have an opinion about it.

—Nick Farina in http://nfarina.com/post/8239634061/ios-to-android

Java was like having a rich lawyer as a brother. He was fun when he was younger, but now he’s a black hole that sucks away all the joy in a 100-mile radius.

—Bruce A. Tate - Seven Languages in Seven Weeks

Why NoSQL is bad for startups

 

April 1st, 2010 by kowsik

We launched pcapr over a year ago now with just a few of us working part time to build and manage the site. pcapr is powered by CouchDB, a NoSQL database written in Erlang with JavaScript as the primary query language. Frankly, this has been a disaster. We are planning on rebuilding the site with Java, Hibernate and MySQL for a number of reasons. [read more]

(Source: nosql-database.org)

… if your only tool is a hammer, every problem looks like a nail.

—Reuven M. Lerner in “At the Forge - NoSQL? I’d Prefer SomeSQL”

Snippet: Running Tasks in the Background While Updating the UI

This is just a very quick note about multithreading in iOS 4.0+. If you want to put a long-running task to the background but keep your users updated about the state of the task you can use this snippet.

// start background execution
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
      // do background work here (e.g. in a loop)
            
      // everytime you want to update the ui call this
      dispatch_async(dispatch_get_main_queue(), ^{
                
            // update your ui here .. e.g.
            [myUIProgressView setProgress:myProgress];
            // you need to provide these variables yourself of course ;)
                
      });
});

ComboBox like UITableViewCells

When I came across the Twitter App for iPhone, I saw something I immediately wanted to have for my own app: UITableViewCells acting like ComboBoxes, opening on tap, closing when a new option is chosen. The idea was so nice that I decided to write a small prototype which can now be found at a public github repository of mine. There is also some basic documentation that might be useful for those who want to re-implement this code.

The key facts in a nutshell:

  • You need a custom UITableViewCell in order to display a status indicator as shown in the “screenshot” above and to make it easier to maintain a status within the UITableViewCell itself. There are plenty of tutorials on the web demonstrating how to create those custom UITableViewCells and loading them from a XIB file.
  • Everytime you change the rowcount in a section (insert or delete cells) you need to adjust the number that ‘numberOfRowsInSection’ returns so you can check the state of your ComboBox cell and return the number of options in that cell plus 1 for the cell itself if the state is ‘open’. If you don’t adjust this value, you run into an inconsistency exception.
  • Although the option cells are not visible from the start, you need to return these cells from the ‘cellForRowAtIndexPath’ method. A good way to realize a dynamic number of option cells is to make use of the ‘default’ statement in the switch and only specify a static cell for the first row in that section (id 0).
            switch ([indexPath row]) {
                case 0: {
                 
                    // return the ComboBox cell here
                    
                    break;
                }
                default: {
                    
                    // return the Option cells here
                    
                    break;
                }
            }

If you’re interested, feel welcome to check the full prototype and it’s documentation in the repository.

The only thing necessary for the triumph of evil is for good men to do nothing.

—Edmund Burke

Introduction to NSPredicate

As I continued to work on my Rumford1797 app I made heavy usage of an Cocoa framework component called NSPredicate. Most of you that have come across NSPredicate have seen it in combination with CoreData when fetching objects from the persistent store. That was my first point of contact with it as well but lately I discovered that it is also useful for cleansing your code when working with any amount of data stored in structures like NSSet or NSArray.

WHAT IS NSPREDICATE?

NSPredicate is basically a predicate to filter objects. The advantage is, that it can be used to filter many collections of objects, e.g. NSSet or NSArray.

// create an array of (ns)strings
NSArray *looneyTunes = [NSArray arrayWithObjects:@"Bugs Bunny", @"Daffy Duck", @"Elmer Fudd", nil];
	
// create a predicate that looks for objects that begin with the character 'B'
NSPredicate *beginsWithB = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'B'"];
	
// this is where the magic happens
NSArray *looneyTunesThatBeginWithB = [looneyTunes filteredArrayUsingPredicate:beginsWithB];
	
// returns 'Results: 1'
NSLog(@"Results: %d", [looneyTunesThatBeginWithB count]);

EXAMPLES

As you can see, filtering an array by a simple predicate is just a single line of code. Even the most complex predicates work like this. So lets pretend we have an objectset of a class called LooneyTune that has the following structure:

Now we want to retrieve these objects from an array that have 'Bunny' as lastName.

NSArray *looneyTunes = [NSArray arrayWithObjects:bugsBunny,honeyBunny,lolaBunny,elmerFudd,nil];
NSPredicate *lastNameBunny = [NSPredicate predicateWithFormat:@"lastName like[c] 'Bunny'"];
NSArray *bunnies = [looneyTunes filteredArrayUsingPredicate:lastNameBunny];
	
// returns 'Results: 3'
NSLog(@"Results: %d", [bunnies count]);

That was again easy, wasn’t it? The next and last example will show you that it is also very easy to use more complex data types as strings like NSDate in you NSPredicates. Let’s say we want to find all LooneyTunes characters that had their first appearance before November 1st, 1966

NSDate *date1966 = [NSDate dateWithNaturalLanguageString:@"November 1, 1966"];
NSPredicate *appearanceBefore1966 = [NSPredicate predicateWithFormat:@"firstAppearance < %@",date1966];
NSArray *firstLooneyTunes = [looneyTunes filteredArrayUsingPredicate:appearanceBefore1966];
	
// returns 'Results: 2'
NSLog(@"Results: %d", [firstLooneyTunes count]);

ONE MORE THING ..

You might have guessed it but I felt the need to emphasize on this: You are able to combine multiple predicates with logical operators like AND and OR. A quick example will show you what I mean:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"(start >= %@) AND (end <= %@)", earlyDate, lateDate];

This simple line of code is able to extract objects (having the properties start and end) that are valid in a given period which is limited by earlyDate and lateDate. In my experience, this method is faster and much more reliable compared to a ‘for’ loop or similar classical approaches for this purpose.

For more information about NSPredicate and its usage please refer to the Predicates Programming Guide.

(The name LooneyTunes and the LooneyTunes characters which have been used for the code examples are property of Warner Bros.)

NIGHTNIGHT by DEDDY