Switch statement oddity

I’m not sure if this holds in C++ or C#, but I came across this in Objective-C the other day (and it also works in C):

switch (someValue) {
	case aValue:
		// You can't declare a variable here
		break;
	case aDifferentValue: {
		// You can declare a variable here
		break;
	}
}

The curly braces make all the difference.

One of the reasons I prefer Mac programming

Since I’ve started Mac (and iPhone) programming, I’ve come to love it. Not only is Apple’s documentation cleaner, more useful and simpler than MSDN, but even the code itself is much simpler. For example, here’s some C++ (or possibly even straight C) code I put together that formats the current system date and time according to the OS’s (or user’s) locale settings and stores it in a const char* called dateString.

Read the rest of this entry »

Thoughts on the Mac App Store

At Apple’s ‘Back to the Mac’ event this week, they announced the App Store for the Mac and in typical Apple fashion, demonstrated it with plenty of superlatives. The main features they are marketing are:

  • It becomes easier to find the Apps you want.
  • Apps install easily (similarly to iOS), and then jump into your Dock where they’re ready to go.
  • App licences cover every Mac you own.
  • You can redownload Apps free of charge.
  • All your Apps are kept up to date.

As amazing as this might be for a desktop OS, Linux aficionados everywhere are claiming that Linux has had this for years in the form of software repositories – Apt, Yum, Pacman, etc. What the repositories don’t have that the App Store will, at least in my experience, is screenshots, reviews, and paid applications. However if this is all the App store is offering, it falls short of some user needs, as well as a whole bunch of developer needs.

Read the rest of this entry »

Reading NSStrings from NSData

I’m in the middle of updating the TF2 Backpack Viewer, which I hope to have done by the time iOS 4.2 ships. One of the new features I’m planning on adding is better friends list integration, and with that comes friend statuses – Online, Offline, In-Game, etc. For friends who are “In-Game”, I’m currently working on getting the server information and then displaying it on your iPhone, iPod or iPad.

Of course, with that came the challenge of querying the server and interpreting the response. And once (thanks to Apple’s UDPEcho sample code) I had the reply data, I had to extract the salient bits of the NSData response. Since the strings in the response are of variable length, this looked like it could be a hard time with pointers and C strings, until I noticed one very valuable function in the NSData docs – rangeOfData:options:range.

Read the rest of this entry »

YSFeedParser

I was recently trying to build an iPhone app (for personal use) that reads an RSS feed, and then discovered just how painful it is to parse XML. I’ve tried to do this before in various languages (all with horrible documentation) but only after discovering NSXMLParser did it finally click how to parse an entire feed into something usable.

To not have the headache of parsing XML every time I want to build something that parses a feed, I created a wrapper for NSXMLParser and NSXMLParserDelegate to streamline the reading of an RSS feed or Atom feed. I’ve only tested it on a handful of feeds, and it does have a couple of shortcomings such as not being able to handle multiple sub-elements of the same name. If I have a leak, feel free to point it out.
Read the rest of this entry »

External IP from command-line

At first I thought this would require a regular expression, until I discovered the folks at WhatIsMyIP.com put together an extremely no-frills version of their service for automation.

Linux users replace curl with wget -O - (That’s a capital O, not a zero.)

Put this in ~/.profile (~/.bashrc doesn’t seem to get run when Terminal.app is opened, your mileage may vary):

function externalip ()
{
    curl http://www.whatismyip.com/automation/n09230945.asp
    echo
}

Welcome

Welcome to Println and Packets.

This blog is about my programming experiences (or experiments), networking, and any crazy hacks I come up with. I’ll probably also post thoughts on news items I find interesting and engaging, as well as ranting about any issues I feel deeply about.

Follow

Get every new post delivered to your Inbox.