Posts Tagged: objective-c programming


27
Jul 09

Objective-C Tutorial – Helpful Debugging By Overriding The Description Method

If you have ever coded Java, you know how important the toString() method is.  Well, objective-c has a similar method called Description. What overriding the description will allow you to do is, customize the output when you want to print your object.

Let’s take an example where you might have a class called User that contains a few properties.

@interface User : NSObject {
	NSInteger userID;
	NSString  *name;
	NSString  *website;
}
@end

Now, at this point if we did

NSLog(@"%@",user);

we would end up with output like

User: 0xd18f50

which is not very helpful.  What would be better is when we want to print the user for it to display their userID, name, and website.  This can be done by overriding the description method like this.

-(NSString *) description {
	return [NSString stringWithFormat:@"ID: %d\nName: %@\nWebsite: %@\n",
			userID,name,website];
}

Now when we call

NSLog(@"%@",user);

is will nicely display this:

ID: 42
Name: brandontreb
Website: http://brandontreb.com

Note: NSLog is your best friend when debugging any objective-c application. Use it often. Also, to open up the Debugger Console, click Run -> Console.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!


25
Feb 09

Why Many iPhone Apps Suck

I have been downloading many different iPhone apps lately and have noticed that many of them suck.

When I say they suck, I’m not necesarily referring to the content of the app. What I am talking about is the programming of the app. But Brandon, how do you know the programming sucks. Well, frequent crashing is an obvious indicator. Also, taking too long to do various computations as well as overall awkwardness.

This is because many non-programmers or hobby programmers decide they want to make an iPhone app without first learning the objective-c language. People just use jank copy and pasted code frankensteined from different examples, close their eyes and pray. This is the architecture of many iPhone apps.

So, who’s fault is this? Well, at first I wanted to say Apple for their lack of tutorials/explanations. After thinking about it, I feel it’s simply lazy programmers driven by trying to make money rather than the desire create a solid and useful applications. It’s quite sad actually.

One challenge here is the iPhone is a terrible platform to learn programming with. The forced program design assumes you have a solid understaning of object oriented programming design patterns as well as many other advanced programming topics. Many CS students don’t even get this until their second year in college! So how can a novice programmer jump right in and make an iPhone app? They code a pile of crap.

So what’s the solution to this? Well I’ll tell you. Teaching people objective-c from the ground up (that, and Apple being more selective when approving apps). I intend to write a whole series of beginner objective-c tutorials (using the mac as a platform rather than the iPhone). I’m not sure yet if these will be posted here or on icodeblog.com. I have yet to decide.

So stay tuned for the first Mac Application Development tutorials.