ajh1138

Cocoa/Objective C development tips for .Net Folks: Part I

In Programming, Uncategorized on October 1, 2009 at 3:41 am

No, this isn’t about using .Net to make iPhone apps!  It’s just a set of observations that might make it easier for people like me who currently use .Net (specifically, ASP.Net) at work, but want to start fiddling around with some Objective C, Cocoa, etc.

Keep in mind that this is from the perspective of someone who got into web development without traveling down the formal education route (when some of my buddies were learning C, COBOL and PASCAL in CompSci 101, I was sucking sand through a gasmask, patrolling some godforsaken ammo dump or airfield in the middle of nowhere).  I have no pure C language knowledge or insight, so all of this stuff is new to me.  I know many web developers in the same situation, so maybe this will help some people make the connections between Obj C and the types of languages they’re used to.

- “Actions” are event handlers.
- “Outlets” are properties that can be bound to your user interface widgets.  They are objects of the same type as the given widget. So if you have a RoundedButton on your UI, you’ll probably have a RoundedButton in your .h/.m code if you want it to actually do something.

From what I can tell, you don’t need to name the specific buttons in the UI part…you associate the button with its corresponding outlet via a drag-and-drop in the UI editor (“Interface Builder”).  I’m pretty sure you can do it manually/dynamically via code, too.  In the .Net world, you’d set the name of the button in the UI editor or in your HTML code, and Visual Studio would automatically make the corresponding code additions.  I haven’t seen that ability in XCode/Interface Builder, but I’m totally new to this so don’t take it to mean that it doesn’t happen.

You can think of the .xib file (known as a “nib” file by Cocoa vets to honor the former file extension) as needing a code-behind page.  The nib is your UI, and the .h and .m files need to be told “Hey, I have these do-dads on the UI.”  After all, if you dig through the code that ASP.Net automatically generates, you’ll find the same kind of definitions for each UI item in the code-behinds.  Same-same.  Don’t let the EXTREMELY WEIRD BRACKET-HEAVY SYNTAX scare you.

Speaking of which, here’s a brief bit of learnin’ about the syntax.  Let’s take apart this function…

-(IBAction) openThing: (id) sender
{
// code that does stuff
}

The hyphen is there on purpose.  (Don’t ask me why.)  (IBAction) is the return type, openThing is the name of the function.  (id) is the type of the argument and sender is the argument itself.  To call a function, use brackets like this:
[myObject doSomething:5];
myObject being the object, doSomething is the function being called, and 5 is the argument.

By the way, the “id” type in Obj C corresponds to Object in our world.  It could be anything.  You’ll need to cast it to whatever type actually sent the event if you want to use it.

If you ask me, the brackets clutter up things, and dot notation isn’t supported enough in Objective C.  They added it to 2.0, but I think it could be used more than it is.  You still need to use brackets for functions if I’m not mistaken.  Of course, you can’t just change the entire syntax of a language without peeving your developer base!

On a side note, I’ve found XCode to be a sweet IDE to work in.  At first I found it a little annoying to have to switch between two completely separate programs to do coding vs. UI work, but then I realized some benefits to it.  Unlike Visual Studio, you can open up just the stuff you want to work on.  VS gets more bloated and resource-hoggy with every iteration, and it would be nice if you could completely rip out the Design mode when you’re working on ASP stuff.  You know it’s lurking in memory somewhere, ready to reformat your clean, hand-written tags the moment that you accidentally summon it.

Another advantage to having two separate programs is that it reinforces the separation of concerns between code and UI.  This premise lies at the very heart of how iPhone programming works – it uses the Model-View-Controller pattern!  I have used this pattern extensively, and although I’m not a design patterns guy, I have to say that it makes for fairly clean distinctions between the various pieces in an app.  If you haven’t used it yet and you’re an ASP.Net developer, you’re in luck.  ASP.Net has an official MVC implementation!  Grab it, build some stuff with it.  Understand it. It’ll help you see how things work in the iPhone (“Cocoa Touch”) world.

The books I’m reading on the subject are Beginning iPhone 3 Development by Dave Mark and Jeff LaMarche, and Programming in Objective C 2.0 (2nd Ed.) by Stephen G. Kochan.  I’ve found both of them to be very, very good books.  I will say that getting about halfway through the Objective C book first will make your life a lot easier.  I’m currently switching back and forth between them.

I’ll be posting more stuff as I wrap my head around this.

Shoot _with_ your family, not at them.

In Airsoft on September 29, 2009 at 3:47 am

I’ve been itching to teach my sons about my favorite sport – shooting – since before they were born.  (And if I had any daughters, you can be darn sure they’d be trained in the ways of firearms as well.)  They’re 8 and 10 years old now, and I’m not quite comfortable with the idea of taking them to a real shooting range to fire real guns just yet.

My dad bought me a .22 bolt-action rifle when I was 7 (a Sears-branded Savage if I remember correctly), and he took me to a small range outside of Medicine Lodge, KS to teach me which end the bullet came out of.  From what I remember, we didn’t cover sight picture, breath control, maintenance, or any other aspects of shooting besides basic bolt-action operation, how to load the 6-shot magazine, and most important of all: SAFETY.

I was naturally inclined to be cautious around, and interested in, guns.  So my dad was able to take me to the range at an early age.  My boys, however, are going to need a bit more muzzle discipline and training in range safety, etiquette and protocol before they’re ready for real shooting.

I am a huge gun safety freak.  My dad is a hunter safety instructor, and has always reinforced the proper ways of handling firearms.  I hope my boys will have that same sense of reverence about guns – so that even if they don’t take a shine to the sport they still know how to handle a gun safely and effectively if they have to.  To that end, we bought four airsoft guns a few weeks ago, one for each member of the family.

My wife and the boys got Crosman p9 pistols, which are worthy copies of the Walther P99.   They’re very accurate, which is important for instilling confidence in new shooters and teaching proper sight picture.  I told the boys that the front fight is like a mouse pointer that you put right under the target, and just make sure it’s between the rear sights.  It seems that my advice worked.  They’re becoming very good shots.  My wife is a natural dead shot, and while she doesn’t spend as much time in our makeshift range she definitely tears it up when she’s there.

The P9 comes with a really nice holster, too.  Bonus!

I got a Crosman R34, which is an eerily accurate copy of the short, collapsible-stock version of the venerable M-16 assault rifle.  I cannot say enough good things about the R34.  It’s just like the guns I carried in the Air Force when I was a Security Specialist.  I carried the standard, regular size old-school M-16A just about every day for the duration of my 4 years in the service, and when I hold that short version I find myself absently fiddling with the foregrip retaining ring or tugging on the magazine to make sure it’s seated, just like the old days.  In the Air Force this shorty M-16 is known as a GAU (also referred to as a “Commando” or XM-177).

The R34 is a single-shot, spring operated airsoft rifle that cocks uses the charging handle, just like the real thing.  The safety has 3 positions and is a tad flimsy, but for $39 USD I cannot complain, especially considering how great the rest of the gun is.  That retaining ring I mentioned actually pulls back to release the foregrips.  The spring tension and feel of the magazine ejector button is spot-on.  When you cock it, the spring makes a tonnnnngggg sound that reminds me of a real M-16 when it fires.  The foregrips have Picatinny rails, which makes for easy mounting of a variety of accessories, and it even comes with an adjustable combat-style sight that you can mount on the top rail.

OK, enough gushing about the R34.  It shoots high and to the right, and I’m still trying to fiddle with the sights.

I set up a makeshift range in our garage that puts the targets at about 5 or 6 meters.  We bought a Crosman electronic target and it provides good feedback for adjusting fire.  I don’t want to seem like a Crosman shill here…that’s pretty much all they sold at the sporting goods store we went to, and frankly I’ve always been impressed with the quality of their stuff.

I also set up some fun targets to shoot.  A small metal canister (I think it was used for developing film) provides a pleasant TING! when you shoot it.  I hung up some old, scratched up data CDs on the target stand with some duct tape.  They’re great to blast apart.  If you spraypaint them black, they also provide good feedback kinda like metal silhouette targets.

While it’s a fun hobby, the boys know that it’s not playtime.  The first day we were shooting, my 10 year old shot me in the thumb as I was adjusting the electronic target.  There’s a reason Crosman calls their guns “Stingers” – that sumbitch hurt!  After the requisite cursing (not directed *at* him, mind you), I asked him “What if we were at a real range, and that was a real gun?”  He was already mortified, and that question really got to him.  I’m glad it happened.  It was well worth getting a (large) welt on my thumb for a couple of days.

You see, I had already breifed the boys on range safety, what you’re supposed to do when somebody goes downrange, etc.  But that incident drove the point home.  It’ll stick.  I think within a couple of years, my older boy will be ready to fire on a real range and the younger boy soon after.

This new hobby has been a real treat.  The guns are cheap (well…you can actually sink a LOT of money into the mid-to-high-end models), the ammo’s cheap, I get to hang out with my kids, and I get to go shooting whenever I want!  And they’re learning safe habits and vital knowledge that will help them when they’re older.

Backup your iPhone data. Right now.

In Uncategorized on September 27, 2009 at 7:44 pm

Fortunately I didn’t have much on my iPhone.  What I did have was completely lost today.  It started with a  failed iPhone update (3.0 to 3.1).  My phone got bricked, and I frantically scoured the net for solutions to the problem.

It seems a lot of users on both Mac and Windows get this “1604″ error when trying to perform an update on their iPhone/iPod Touch.  Honestly, I expect Apple stuff to work a lot more smoothly.  My wife and I *always* have trouble whenever there’s an OS update to our phones.  That’s why I had waited so long before finally grabbing 3.1.

I uninstalled and reinstalled iTunes about 5 times I think, as was suggested by several sites on teh intartubes, and even Apple’s support site.  Eventually, I had to boot my PC into Mac mode to get it to work.  (Yes, it’s a freakin’ Hackintosh.  I have it just for situations like this, and for testing web sites.)

I had a mistaken assumption that when iTunes synchronizes the phone will use the data on the computer instead of clobbering the computer’s data with whatever’s on the iPhone.  It turns out that you can restore your iPhone from an earlier backup just by right-clicking on it in iTunes…but you need to have data to restore from.  Over the course of several re-installs, I found out where my backup data was (on XP it’s in c:\documents and settings\yourusername\application data\apple computer\MobileSync\Backup\{big long string of characters}) and I moved my most recent data onto my desktop for safekeeping.  So when iTunes started up again, it saw that there was no recent folder in there and created a new one.  Later on I was trying to figure out how to restore my old data, and I move my folder back into the Backups folder and restared iTunes.  Then I synced data with the phone…then I cried.  Well, I didn’t actually cry, but I was kicking myself for moving the actual folder in there instead of a copy.  It got clobbered with blankness.

SO…go backup your iPhone data right now before you do any updates, etc.  Go to the path I mentioned above and make a COPY of your backup folders and put them somewhere safe (backup your backups).  Learn how to restore your iPhone data from backup.  Then, write Apple a letter telling them to make updates a little smoother.