Tuesday, March 30, 2010

Objective-C Tuesdays: static variables in functions

Welcome back to Objective-C Tuesdays after a long hiatus. Picking up from where we left off, today we'll look at static variables declared at function scope.

Normal local variables are declared inside the body of a function:
int countCharInString(char c, char const *s) {
  int count = 0; // plain old local variable
  do {
    if (*s == c) ++count;
  } while (*s++);
  return count;
  // after we return, count ceases to exist
}
Plain old local variables exist only for the time that the function is executing. When the function returns, local variables vanish.

Sometimes you want to keep a value around between calls to a function. Typically you use a global variable for this, but if the variable is only ever used inside a single function, you can use a static variable declared inside the function instead. One common use for this is to automatically initialize a singleton object or data structure before its first use. For example:
// static variable declared in a function
NSArray *getDaysOfTheWeek() {
  static NSArray *daysOfTheWeek = nil;
  if ( ! daysOfTheWeek) {
    daysOfTheWeek = [[NSArray alloc] initWithObjects:@"Sunday",
                     @"Monday", @"Tuesday", @"Wednesday", 
                     @"Thursday", @"Friday", @"Saturday", nil];
  }
  return daysOfTheWeek;
}
The static variable daysOfTheWeek is actually a global variable and follows the same rules as global variables with one difference: it's only visible inside the getDaysOfTheWeek() function. This makes it seem like some magical kind of local variable that keeps its value between function calls, but in fact, you can rewrite getDaysOfTheWeek() to use a normal static global like this:
// same function using a static global variable instead
static NSArray *daysOfTheWeek = nil;

NSArray *getDaysOfTheWeek() {
  if ( ! daysOfTheWeek) {
    daysOfTheWeek = [[NSArray alloc] initWithObjects:@"Sunday",
                     @"Monday", @"Tuesday", @"Wednesday", 
                     @"Thursday", @"Friday", @"Saturday", nil];
  }
  return daysOfTheWeek;
}
There are only a couple of minor advantages to using a static variable declared in a function rather than a normal global:
  1. the static variable lives near the code that uses it
  2. global variable name clashes are avoided
However, because it's a global variable, it suffers from all the problems that global variables do. Code that make extensive use of global variables is often harder to understand and difficult to reuse. In a multithreaded application, global variables need to be carefully synchronized between threads to avoid data corruption and deadlocks. Global variables also make writing automated tests more difficult and sometimes even impossible. In general, it's best to avoid using global variables.

Next time, we'll take a look at local variables and function parameters.

Monday, March 29, 2010

Apple Announces iPhone SDK 3.2 GM Seed for iPad now available

I just received an email from Apple stating the iPhone SDK 3.2 is now available and that all apps submitted for iPad must be built with this latest release.

So iPhone and iPod touch app developers, have you downloaded the SDK and have you started a migration plan for the iPad?  It's an open-market for now without all of the competition so get in while the getting's good.

Not sure if you want an iPad?

Still on the fence about getting an iPad? Check out the guided tours videos that Apple recently posted. The short videos give highlights of the built-in iPad apps as well as the iPad versions of Keynote, Pages and Numbers.

A Review Of Features
Safari:  Redesigned to look and act more like its desktop sibling.

Mail:  Now supporting a two column layout in Landscape (wide) mode Mail looks a little more user friendly.

Photos:  More like iPhoto with grouping and viewing this is a welcome change to the Photos app of the iPhone.  I love the "pinch to peek" feature letting you look into a stack of images.  Slideshow is part of the iPhone and iPod touch but on the iPad it has been updated to work like iPhoto and makes a great digital photo-frame.  It also has iPhotos Faces and Places features as well as the ability to import from a digital camera or SD card (with 30-pin connecter attachments sold separately).

Videos:  Much like the iPhone and iPod touch counterparts it it displays what you have synched up from iTunes.  A notable feature is that it contains the summary, actors, producers and additional credits for you to review.

YouTube:  One of the hottest video sharing properties on the net is YouTube and the native YouTube app on the iPad is a wonderful counterpart.  For those of you who love YouTube this is a great experience.

iPod:  Now the application itself is more than on the iPhone and iPod touch with an iTunes like way to find and play your music.  But fitting an iPad in your pocket may be a little hard for most of us.  One question I have is will it support AirTunes?

iTunes:  We all know that iTunes is the leader in online media/content distribution/purchasing and that most competing products use it as a feature list.  The iPad version brings the mobile entertainment experience a little closer.  My favorite feature is iTunes U.

iBooks:  A new step in the publishing arena, Apple hopes to capture traditional literature and move it forward the same way it has for music, movies and television shows.  This is one of the main things I am looking at the iPad for, a way to carry around the books I want to have with me without breaking my back.  It sports a built in English dictionary and the ePub format so that you can change the font and font size.  Will this be a Kindle killer?  Only time will tell.

iWork on iPad
Keynote:  Part of the iWork suite of apps, Keynote is an easy and intuitive way to create and present beautiful presentations.  It works with the desktop version as well as PowerPoint.  There is an optional adapter to hook up the iPad to a projector or television.  You can share it via email, upload it to iWork.com or export it as a Keynote or PDF file.  I am looking forward to this app and once I start to use it I'll have to decide where to do presentation work, on a desktop, notebook or the iPad.

Pages:  The second of the iWork suite or apps, Pages works as both a word processor and a page layout tool. It too works with iWork.com and will import Word files.  Also I just found out that not only can you use the new Keyboard-Dock but that it will work with an Apple Wireless Keyboard.  Score one for productivity.  I'll be picking this up as well.

Numbers:  The last of the iWork suite is Numbers, an Excel type of spreadsheet application (it works with Excel files too).  I love Numbers for the desktop with its special controls like sliders and pickers and that it reformats my graphs and charts in real-time as I make changes to my dataset.  The iPad version of Numbers looks to be simplified in it's UI but still has all of the bells and whistles as its desktop counterpart.  It even has a new keyboard designed for creating advanced formulas.  Even without a mouse, the thought that has gone into the UI makes editing, formatting and layout of spreadsheet simple.  It also has a Form view for your spreadsheet, akin to the Form view of a Google Docs spreadsheet.  I will also pick up iWork for the iPad.

Apple iPad: The future of mobile computing

Soon, very soon, Apple will be releasing their latest mobile device, the iPad. It basically looks like a iPhone-ized version of the screen of the old 12" MacBook.  It's the start of a sleek new line that will in time support additional features such as a camera and live video conferencing (I'm hoping).


What excites me about the iPad is its screen resolution of 1024 x 768 pixels, which should sound familiar to most of you. It's the magic 4:3 ratio of the 90s that most programers and designers are familiar with using as the lowest common denominator for their design.  But unlike the CRTs of yesteryear, the iPad has 132 ppi - almost twice the number of pixels packed into each inch making for a beautifully sharp image.

The iPhone is incredibly useful even at a resolution of 480 x 320 thanks to Apple's guidelines and Cocoa Touch that encourage apps to build standard infinite drill-down menus; a master-detail-detail-detail... view.  But the iPhone is constrained by its size - an issue that does not plague the upcoming iPad.

This will allow a return to old school size application where there is more information and context presented for us to consume and interact with.  Game developers will finally be able to get their level maps on the screen and not have to resort to "pinch and spread" trickery for zooming to see the map.  For business app developers (or almost any software developer) there is 65% - 75% of the screen real estate of a common high end laptop or desktop.



But when will I be in an environment where it makes practical sense to have an iPad over a notebook computer?  This is a valid question indeed and one that I've heard numerous times.  The iPad is not here to replace the notebook computer, nor could it - yet.  Rather it's here to augment it by filling in a gap that netbooks have not succeeded in doing for most users.

Over the past years we have seen desktop computers give way to laptop/notebook computers for most day to day activities; and this will probably not change for a while.  We have also seen the evolution from server farms to virtualized instances running on a single server to cloud computing.  At this point the computer is just a screen connecting to a hosted web service.  A wireless mobile monitor that weighs about a pound and a half.

An iPhone is not large enough to expedite a change in this evolution, but the iPad is.  Already I am hearing about and talking with people of the future of computing, the evolution of the interface and beginning of convergence.  It's all fantastical talk and nothing is real until is available for us to use, but it is coming.

Where developers were sacrificing usability quality due to an itsy-bitsy space they can now step up and design basically for the desktop - and if they set up their project and build systems properly they can end up there as well.  No longer tied to a small window of opportunity, the future of mobile computing will be big and the smart developers will seize this opportunity for their own.  For not only is the iPad a bigger space to work with, but it's also less crowded by apps and other developers.

I wonder how many of you developers are thinking of changing your focus from iPhone to iPad - with an iPhone story as well?  We know that we are and we are thrilled to be a part of this exciting new time.

What are your thoughts?  We'd like to hear what you have to say on this subject.  Are you a consumer purchasing an iPad and why?  Are you a developer making the change to the iPad because it's less competitive now?  Are you a marketing director for a software product who is thinking about porting to the iPad? Do you think that this a bunch of hype and the iPad will die a slow tortured death like the Apple TV?  Whoever you are share your thoughts with the world!

Friday, March 26, 2010

iPhone Friday - March 26, 2010

Happy Friday everyone!  I am continuing last weeks adventure into the delicious world of kaiseki with four additional wallpapers.  Kanpai!




Monday, March 22, 2010

Apple offers App Store app gifting

Today I read a blog post from Rana Sobhany who noticed that the iTunes Store Terms and Conditions had changed, and in it Apple quietly announced that they have added App Store "gifting" which makes me (and other developers) quite happy.

As a developer when you add or update an app in the app store you are given 50 promo codes to use which is not a lot if you need to do a wide marketing campaign.  Many small developers and companies have turned to iTunes Gift Cards as a way to reimburse testers or when providing outreach for additional reporters, but how do you go about getting a .99¢ gift card.  You don't.  You have to buy a larger denomination - cutting into your profits.

Developers rejoice as you can now gift the app.  Sure you have to pay for it, but you can do a one off and it will only be the .99¢.  Thank you apple.  Now about the new MacBook Pro...

iPhone Tech Talks 2009 videos now available

Last year Don and I attended the 2009 iPhone Tech Talks and had a very good time.  There were three tracks and Don and I split the tracks based on our interests and understand of the presentations.  But alas there were more presentations than we could attend.

Today Apple announced that the videos of the iPhone Tech Talks are now available to registered iPhone developers.  So, if you didn't get a chance to attend the events in person, you should take a look at the videos and see what knowledge you can glean from them.

Developers, here's the link.  Cheers!

Friday, March 19, 2010

iPhone Friday - March 19, 2010

Hello and happy iPhone Friday!  Today's collection of wallpapers come from a delicious, home-catered kaiseki (会席料理) dinner I recently had with Japanese chef Katsunobu Yamabata, who friends affectionally refer to as Yama-san.

The experience was wonderful and it was an honor to me one of the six guests and to watch a master at work.  I hope you enjoy the pictures of the dishes as much as I enjoyed eating them.  (^_^)





I will post the other four wallpapers of the other courses next week.  Kanpai!

Wednesday, March 17, 2010

iPhone SDK 3.2 Beta 5 now available

Today Apple released the iPhone SDK 3.2 beta 5 for it's developers.



If you have not yet jumped on board and started developing for the iPhone, iPod touch or iPad I have just one question for you, "why?"  At only $99.00 a year to develop for the platform it's a low cost for entry, the only thing you'll need to know if you don't already is Objective-C and Cocoa Touch (to start).

If you are a developer, hit the iPhone Developer Center and download the latest beta now.

iTunes Connect - Schedule Price Changes for Your App

Today Apple announced a new feature in iTunes Connect allowing you to schedule changes to the price of your app, including In App Purchase items.
This feature is designed so you can set a single price change or consecutive price changes all in advance.  -Apple
This is a great feature for scheduling sales, promotions, congruent press or even just to make a price change when you're "unavailable" to do it by hand.

I do like that Apple is continuing to add new features and to create a better experience for their developers.  Thanks Apple.

Friday, March 12, 2010

iPad: Now available to pre-order or reserve and pick up

Starting today the latest of apples mobile devices, the much hyped (magical and revolutionary) Apple iPad (Wi-Fi) is now available for online pre-ordering.  Starting today you can either order it online and have it delivered to your home or office on April 3 or you can reserve it and pick it up between 9:00 AM and 3:00 AM on April 3.

The iPad (Wi-Fi + 3G) is available for pre-order only and ships late April.  So the question is, do you get the Wi-Fi version now or wait for Wi-Fi + 3G and which memory option: 16GB, 32GB or 64GB?

iPhone Friday - March 12, 2010

Hello and happy (iPhone) Friday.  Today's iPhone and iPod touch wallpapers are from a collection of paisley patterns I designed for a project quite some time ago.  I'm glad that they could have a little spring treatment applied to them.  Hopefully you'll enjoy them as well.  Cheers!






Tuesday, March 9, 2010

iPhone SDK 3.2 Beta 4 now available

I just took a look at my inbox only to find a message from Apple Developer Connection: iPhone SDK 3.2 beta 4 For iPad Development Now Available...


Apple iPhone developers download it here.

It looks as if we're entering the last mile of beta releases with a final on the horizon and an iPad ship date set for April 3rd.  Are you ready?  Have you started porting any of your apps for the iPad?

Sunday, March 7, 2010

iPad release date set for April 3

I just checked Apple's website and noticed a little change on their home page:



It would seem that Apple has set an official date for the release of the magical and revolutionary new device: the Apple iPad.

Will you be in line?  Will you wait a month for 3G?  Or will you be waiting for next year's iPad, the one with the purposed camera and iChat?  Let us know.  Comment below.