Developing for iPhone (and UIScrollView Example)

So, what have I been doing lately? Well I’ve been wrestling with my first Mobile App in Objective C and the Cocoa Touch frame work for every bodies’ favourite mobile platform!

Between a day job, a social life and a couple of other projects, I haven’t really had the time I need to really get to grips with iPhone as a development platform yet… one of the biggest things that is holding me (and I’m sure a lot of other developers) back is the limited documentation that’s available.

What you get from Apple with the SDK (both actual documents and help given by XCode’s research assistant) is very good but it suffers from being single sourced. That is to say that there are no other contributors in the periphery giving comments, suggestions or even pointing out mistakes with the official docs. This is partly because of the infamous iPhone SDK NDA (which fortunately as now being lifted) but it’s also down to the fact it’s a relatively new and proprietary technology…

A good example of this is the problem I had getting the UIScrollView class to work. The documentation was accurate to a ‘T’ and Apple even provided some fairly complex implementations to demonstrate how to use it with paging and zooming… but there was no example or explanation of how the fundamentals worked. I was left with the impression by the sample code that I needed to set up a delegate to handle all the events that the class actually does straight out the box! An hour later I was still playing around with implementations of the UIScrollViewDelegate protocol, trying to get my scroll bars to work when, all the while, all I needed to do was tell my UIScrollView the full dimensions of its content!

All I’d have needed to see was the 4 line example I’ve drawn up below, in the absence of Head First iPhone Development, I’ve even made a simple colour diagram showing the relationship between the two areas you have to specify! I doubt something so simple will help many people in the future, but you never know… and I hope this will be just the first example I write up for my blog!





uiscrollview diagram


//all variables are of type CGFloat, unless named otherwise! 

UIScrollView *myScrollViewObj = [
    [UIScrollView alloc] initWithFrame:
          CGRectMake(frameX, frameY, frameWidth, frameHeight)
];

[myScrollViewObj setContentSize:
     CGSizeMake(contentWidth, contentHeight)
];

[myParentViewObj addSubview:myScrollViewObj];
[myScrollViewObj release];



PS. Hey Steve, please don’t sue me! The NDA’s being lifted anyway right? :)

Great Interview Question

So, how this for an interesting interview question:

You have two variables with integer values, without creating a third variable exchange their values.

At first it sounds fairly simple, but then it hits you that it’s actually not… there’s no simple ‘exchange value’ function. Here’s how I solved it:


list($a, $b) = array($b, $a);

My answer provoked a surprised (if not bemused) Yeah, that’d work… I’ve never heard that one before look. After a little conversation it turned out that the answer they were expecting but rarely got was:


$a = $a + $b;
$b = $a - $b;
$a = $a - $b;

My answer is of course better, because it works with any type of variable, but what they were actually testing for was a logical brain that would solve the problem with simple maths… The fact that most candidates don’t even try answering the question goes to show how dastardly it is!