Archives for : May2009

Summer Plans

Now that I get to skip JavaOne for the first time in five years (more on that in a couple weeks), I have a short conference schedule for this summer.

  • Apple WWDC – June 8-12 – Expensive, but so worth it. The nature of the Mac and iPhone development community is, honestly, that of a Cargo Cult: it’s primarily driven by Apple’s decisions and announcements (and I don’t think that’s a bad thing; inclusion and community sounds great in theory, but sometimes the result is a four-year pissing match over closures in Java, or a competition of multiple awful Linux desktop environments, each awful in its own special way). So attendees get an advantage by having direct access to the essential APIs and frameworks, both in the form of sessions and labs with the engineers. There’s a lot in QuickTime and Core Audio that seems to come out of the blue, but you get the thinking behind it when the Apple guys present it in a session.

    There’s also a lot of information here that seems to never get out to the public. For example, last year’s Media and Graphics State of the Union announced the deprecation of QuickTime for Java, but no public announcement was ever made, and the QTJ home page, while dated, still goads developers into adopting it.

  • iPhone Camp Atlanta 2009 – July 18 – It’s a heck of a drive, but we moved out of Atlanta just last Fall, and the sale of our house there is finally closing three days before, so this half-day unconference affords a chance to pick up any paperwork or forgotten personal effects, to say nothing of meeting up with other iPhone devs. I proposed via Twitter a session on low-level Core Audio, something I’ve had my head a lot in this Spring.

    Right now, there are over 100 registered attendees, though I’d be surprised if this many show up (people will always register for something free, then half will flake the day of… I would have had a nominal [$25-50] registration fee just to weed out the flakes).

I’m also thinking about using one of the whiteboards at WWDC to propose the idea of a Core Audio unconference somewhere. A lot of people are digging into CA on the iPhone (probably out of necessity… in 2.0, the Audio Queue was the only way to play a flat audio file, and as of 2.2, recording still requires AQ [or audio units]), at different levels of experience and ambition. Maybe it makes sense to get together somewhere for a few days, share notes, and bang on code. We’ll see if anyone bites.

Fun with varargs

For reasons you don’t need to know about (yet), I wanted to get my usual crutch of a logging UITextview implemented in plain C.

I hadn’t wanted to mess with varargs, so I usually write an Obj-C method like this:

-(void) screenLog:(NSString*) s {
	textView.text = [NSString stringWithFormat:@"%@%@n",
		textView.text, s];

What this does is to create an autoreleased NSString built from a format that’s just two strings concatenated together — the current contents of the text view and the argument string — and a new-line character. It then sets this new string as the new text of the UITextView

It sucks a little bit to call, because you have to pass in an NSString, not the usual varargs you’d use with NSLog. So to do:

NSLog (@"Current age: %d", 41);

you’d have to build the string up-front, like this:

[self screenLog: [NSString stringWithFormat: @"Current age: %d", 41]];

So, kind of annoying, but still useful when you want to log to the screen instead of standard out, like I’ve had to do this week while doing some Bonjour stuff between multiple devices scattered about the office, at most one of which gets to log to Xcode’s console. Yesterday’s post, with onscreen output of the two devices getting each other’s test message, shows why this is a nice crutch to have for experiments, prototypes, and throwaways.

Anyways, I actually wanted to do this with plain ol’ C, and happened across Matt Gallagher’s great write-up of varargs in Cocoa. Combining that with the realization that NSString has some method signatures that take a va_list, I was able to rewrite my screen logger in plain ol’ C:

void LogToUITextView (UITextView *view, NSString* format, ...) {
	va_list args;
	va_start (args, format);
	NSString* appendedText = [[NSString alloc]
				initWithFormat: format arguments: args];
	view.text = [NSString stringWithFormat:
				 @"%@%@n", view.text, appendedText];
	[appendedText release];

Calling it feels a lot more like calling NSLog:

- (void)viewDidLoad {
    [super viewDidLoad];

	// customize point
	LogToUITextView(textView, @"Current age: %d", 41);
	LogToUITextView(textView, @"Current weight: %3.1f", 243.6);
	LogToUITextView(textView, @"Available fonts:n %@",
				[UIFont familyNames]);

And check it out: it actually works:


I’ll probably adapt the varargs approach in my Obj-C logging function going forwards, but still, it’s nice to be able to make the procedural C call, especially since you could switch all NSLog calls to LogToUITextView with a single global replace.

Update: Here’s an even “more C” version that’s functionally equivalent:

void LogToUITextView (UITextView *view, NSString* format, ...) {
	va_list args;
	va_start (args, format);
	CFStringRef appendedText = CFStringCreateWithFormatAndArguments (
		(CFStringRef) format,
	CFStringRef newText = CFStringCreateWithFormat (
		(CFStringRef) @"%@%@n",
	view.text = (NSString*) newText;
	CFRelease (newText);
	CFRelease (appendedText);

Obviously wordier, and we lose a convenient autorelease, since CoreFoundation doesn’t have autoreleasing.

Bonjour, encore

Let’s see if I can dodge the NDA on this one…

So I’m working on peer-to-peer networking for the new 3.0 chapters in our iPhone book. One thing I believe they’ve pointed out publicly is that the framework works either with Bluetooth (which is the new hotness), or with a connection you provide.

For a number of reasons, I’ve been working on making good with the latter. I figured it would be pretty reasonable to do it with Bonjour. Of course, there’s a reason we only showed client-side Bonjour in the current Network I/O chapter, and then only with HTTP browsing. Bonjour is just about service discovery; to use a service, you talk raw sockets. So while it’s easy enough to get participants to discover each other over wifi, for one or both to be a server, you have to set up and bind a socket, probably using the C-based BSD sockets APIs. Tried and true, but probably pretty scary for our readership.

I started working with the usual stream metaphors, but happened across NSFileHandle, a Cocoa object to wrap Unix file descriptors. If you can set one up with a plain ol’ socket, it does a lot of the asychronous connection management for you. To wit: instead of managing your own little buffer and looping over calls to NSOutputStream‘s write:maxLength:, you can just call a big ol’ writeData:. Plus, you get asynchronous events like connections and new data as NSNotifications. Examples in the Bonjour SDK’s Picture Sharing and Picture Sharing Browser sample code, as well as an O’Reilly article from 2006, How to Write a Cocoa Web Server.

Finally got it working just after lunch. The simulator and a 2nd gen Touch (“Moogle”) discover each other and send each other test messages over a generic “receive some data” method call:

(click for full-size image)

No sample code today; after a hundred false starts, it’s too ragged. I’m going to zip up this working code (it’s in an non-svn’ed “throwaway” directory for experiments), and then plan out something that’s suitable for exposure to daylight.

A word from Wordle

Have my first iPod / Music Library code for iPhone SDK 3.0 working… still adding a few more things before writing the chapter.

While I’m busy, I thought this was amusing: Wordle word clouds of recent activity on this blog:


and my Twitter activity: