Object Chains

January 30, 2009 · Posted in dev · 1 Comment 

This almost certainly isn’t a first, but I discovered a neato trick. Let’s make a chainable class, called Chain. It would contain two instance variables (at least) called next and previous, also of the Chain class. Its init method would go something like this:

- (id)initChainOf:(int)n
{
    if(self = [super init])
    {
        // Setup instance variables, etc. Then:
        n--;
        if(n > 0)
        {
            next = [[Chain alloc] initChainOf:n];
            next.previous = self;
        }
        else
        {
            next = NULL;
        }
        previous = NULL;
    }
    return self;
} 

Call Chain *myNewChain = [[Chain alloc] initChainOf:5]; and you’ve got five new objects linked together. After the first object is initialized, it decrement the link number and initialize another link if the link number is above zero. The next link repeats the process until the link number reaches zero. The instance variable previous (more explicitly called self.previous) is initially set to NULL, but if the link has been created by a parent link, it will manually set next.previous to self, which alone is a slightly confusing statement. Clean up works similarly:

- (void)releaseLink
{
    if(next)
    {
        [next releaseLink];
        [next release];
    }
}

One link will call the next to release its link, which will then call to release its next link, etc. No object actually releases the self.next object until it has told its own link to do the same, so all the releases actually cascade down. The whole thing can be unravelled with two lines:

[myNewChain releaseLink];
[myNewChain release];

Recursive programming is one of those things that’s crude is misused but elegant when done right. I’m not sure which the category the above falls under, but maybe it’s too bare bones to qualify yet. With the above setup, you lose some of the control that NSArray and its mutable counterpart provide. On the other hand, it’s probably leaner and faster at what it does if you don’t need all that control. Sorting is non-existent, inserting objects between links (or inserting any object at all that isn’t a Chain) becomes problematic, but its behaviour is just what I need right now.

Instruments: Building a Watertight App

January 25, 2009 · Posted in dev · Comment 

Probably one of the main criticisms I’ve hear about iPhone applications is that they have a tendency to crash. This is not isolated just to bad applications, not that it helps. If I had to make a wild guess, I’d say the main cause of such crashes is memory leakage combined with the iPhone’s apparent 128MB of memory. Sure, on a desktop with several gigabytes kicking around, you could almost afford a few sloppy leaks (not that you should), but the iPhone offers no such padding. You don’t need a big leak either as long as it keeps growing; add to this that many non-Mac developers have no previous exposure to Cocoa’s alloc-retain-release methods and you’ve got an application ready to tank.

Instruments, part of the developer tools, has a crazily useful module called Leaks that does exactly what it advertises. It keeps track of all memory allocations and deallocations and lets you know where the outstanding balance is and which method it lives in. Although early optimization is considered be on par with supervillainy, leak detection is not optimization per se and I would recommend profiling for it early and regularly. Method calls will only get deeper and more tangled as time goes on. I was surprised to learn that my own code had nine leaks, nine. That’s nine more than I was hoping for. I’ve cut it down to a single 3.5KB leak thanks to Instruments, and while that may not seem like much, I’m going to find and nuke that 3.5KB before the night is over.

Here to Stay: Our Brave New World (Part 1)

January 23, 2009 · Posted in Stuff · 3 Comments 

Gigi has found a fascinating book (as is her habit), The Cult of the Amateur, a book whose arguments boil down to the following: the internet is destroying our culture. Despite that I’m one of the first to shake my head and exclaim, “Oh, internet!” every time something ridiculous happens online, I strongly disagree with Andrew Keen and his silly book. Yes, I’m passing judgement, but to be fair, he started it. I’m not starting where I should, so let’s turn the clock back to Composition 101 at York.

Westcott, the musicianship professor (and excellent ragtime pianist) was following his regular lesson when all of a sudden we realized he was on a tangent: music was dangerously in need of being democratized, he said. The superstar had existed for hundreds of years now thanks to modern roads, the printing press, airmail, and all other methods by which we shrunk our world. If you were Mozart, you were no longer simply famous in only your home town; now you were famous throughout the whole country, then several, and finally worldwide. Michael Jackson is another household name, and even anyone who hasn’t heard his music will recognize the name. The goal for musicians became to compete on the world stage and success was measured against the same. What about local talent? Completely overshadowed. Westcott was now very animated, and the message was clear: take back music from the few elite, put it back into the hands of everyone. Form a community orchestra, join a band, join a choir, perform locally and rise above the siren song of fame. Unfortunately, marketing yourself becomes problematic without the millions of dollars promised by The Industry. Enter the internet.

Arguably, Westcott’s goals have already been accomplished. Music, film, news, open source software and opinion now belong to anyone with an internet connection. To Keen, this is a terrifying notion, and it forms the basis of his book. For example, his claim that today’s generation can’t tell the difference between credible news by objective journalists and what they read on an uninformed blog lacking citations — like this one. In this respect I partially agree with Keen, but I find his implications off-base. Without critical thinking skills, it is difficult to tell the difference between objective journalists, ill-informed opinion, and outright lies. I find it a dangerous idea that we should trust only our “traditional” news media. Traditional media is susceptible to bias, misinformation, and propaganda, to different degrees than independent bloggers. Yes, traditional media has a dedicated staff who draw their salary from doing the job right, but that doesn’t make them bullet proof. Case in point: Keen presenting himself as an objective journalist.

Another target of Keen is Wikipedia. Believe it or not, we’re only on page four of the introduction. We, the reader, are told that none of Wikipedia’s nearly three million entries have ever been edited or vetted for accuracy, which is tantamount slander. Jimmy Wales actually tells us that Wikipedia has been compared favourably to Encarta by traditional magazines (but don’t believe him, I bet no one vetted that claim). Wales even goes on to point out Time Magazine misquoting him, so don’t tell me that Wikipedia is the only one ruining public understanding. Let’s move on though, because we’ve still got a lot to cover.

Ah, YouTube. Among the videos Keen cites is one which he describes as a young woman watching another YouTube user who is watching yet another user, all leading to a woman making a peanut butter and jelly sandwich in front of the television. Read that back a second time. Is that art? Don’t agree? Welcome to all of recorded human history. The concept of art vs. mediocrity isn’t new. Going back to Westcott’s complaint against the superstar, mass media was an enabler to an elite few to become world famous, but world fame did not equate to art, talent, or creativity; just fame (though certainly talent and creativity help). Some people think John Cage is a hack. I think Top 40s radio is a cesspool. Now that we have the internet, there are much deeper depths to plumb, and even if you think most of it is coal, you have to start somewhere to find diamonds. It’s now possible to find alternatives to world-famous superstars, and those who never used to have a chance have now been given an audience.

We’re not done yet though, for Keen is about to play the “think of the children” card. Cited is the brutal war going on between Israel and Hezbollah, juxtaposed against digg’s tendency to publish more banal stories. Keen is now placing the responsibility of publishing the information he wants on sites that publish, well, other things. If you want to read about the latest Ext4 file system, you head to Ars Technica. If you want to watch Stephen Colbert act wilfully ignorant, you hit Comedy Central. If you want world news, you read one of the many respected online publications, many of which have risen from traditional dead tree methods of circulation.  Digg is a news aggregator, not a news substitute. Keen’s point does not escape me though, which is that Digg indicates what is most popular among internet users, and his concern likely rises from the fact that banal stories are more popular than important ones. Actually, it’s my non-expert opinion that this was true before the internet, so let’s move on and wrap up.

One of Keen’s final assertions is that new media such as YouTube and MySpace are starving traditional media of their advertising revenue. This is one of the biggest told-you-sos of the internet: everyone knew there was money to be made on the internet, everyone. Instead of embracing change, the dinosaur that is old media resisted it. Newsprint knew that they were losing customers to internet media: their competitors could produce content inexpensively since there was no physical circulation. Sounds like cheating, doesn’t it? If you think old media should have borrowed this idea instead of fighting it, you just won the internet. Why could old media not play that game too? They did eventually, but too late if you ask me. The music industry is another one that shot itself in the foot for years. Music piracy skyrocketed on the pirates’ assertion that downloading was convenient until someone figured out that people would still pay for the music as long as you made it convenient, and it’s been hugely profitable ever since. Replace “downloading was convenient” with “stealing was convenient” and you see the shutter-vision that prevented this from happening sooner. The movie industry is just the same: internet rental services and internet to TV streaming is more convenient than ever, and the main detractors are not the studios but the consumers who worry that electronic distribution gives too much power to the studios. So have traditional media been robbed of their revenue by the new? Arguably some have, and it’s the price for not keeping pace.

Double Negative

January 13, 2009 · Posted in Stuff · Comment 

It turns out that Where the Hell is Matt is a carefully crafted hoax — except it isn’t. Despite that the only thing going on here is a man dancing his way across the world (with help!) it’s hard not to enjoy. Matt scores huge points for giving us a glimpse of what we’d all rather be doing.

Awkward highlights: The Entertainment Gathering trying to figure out who just got punked worse, and David Pogue coming to terms with his Letterman-esque desk.

Mirror, flip, inverse, invert, stop drop and roll

January 11, 2009 · Posted in dev · 2 Comments 

Not having much to compare it to, I can’t honestly say how well the developer documentation stacks up against those available on other platforms. It certainly serves me well enough, and is worlds apart from what came with MPW. The day that MPW finally started working was a small miracle; this was before Google had an answer to just about everything (great instructional video too), and most libraries assumed you were using CodeWarrior anyway. At any rate, the reason for today’s post: for everything not spelled out by the developer docs, there’s Google.

After spending a good hour trying to trackdown a [UIImage reverse] method (there is none), I’ve boiled it down to a working block of code. Best of all, it’s short! So here’s to you, Googlers, I’m putting it on the internet in hopes that another dev finds it useful. The following snippet will horizontally reverse/flip/mirror a UIImageView:

UIImageView *myView = [[UIImageView alloc] initWithImage:myImage];
myView.transform = CGAffineTransformIdentity;
myView.transform = CGAffineTransformMakeScale(-1.0, 1.0);

That’s it. To flip it back:

myView.transform = CGAffineTransformIdentity;

Not so scary anymore! So what’s going on here? Get your answers from a more authoritative source. It’s a good read about a powerful property, and highly recommended for anyone who doesn’t want to create two or ten different versions of the same image at different rotations.

Breakin’ Stuff

January 11, 2009 · Posted in dev · 2 Comments 

It’s 6am and sleep is proving impossible. My brain won’t shut down, instead thinking about the enormous possibilities made available by the iPhone SDK. Objective-C is back with a vengeance; as is becoming the norm, the main() function is meant to be off-limits, and there isn’t really a substitute for it either. Instead of being a hindrance, it’s a godsend. Draw up an interface, plug in some code, connect the outlets, and bampf. Classes start talking to one another. Instead of growing an increasingly crazy tree of functions pushing values up and down, there’s actually some kind of programmatic dialogue going on. While this probably isn’t a new concept to an experienced programmer, it’s the first times that I’ve found it intuitive.

Anyway, that’s just it. The whole thing has me pretty excited and the brain won’t pipe down, instead busying itself by dreaming up ways to keep classes nicely self-contained and autonomous while actively participating in the overall program. Or, maybe a way to make views handle themselves and report when something important happens, instead of controllers polling them constantly when resources could be better spent.

Not that it’s all unicorns and rainbows. Registering, configuring, and provisioning a test device was like finding out the rainbow is made of sewage, and the unicorn just ate your last pie. You eventually work through it, but probably won’t look forward to doing it a second time.