From PHP to Objective-C

Being a pretty experienced PHP developer, and having a fair knowledge of C (Wrote a distributed computing experiment with 16 of my Dad's NEC microcontrollers), I'd like to make the move to Objective-C and Cocoa, eventually aiming to integrate the Mac / iPhone apps with some of my PHP projects.

Going through the "Cocoa Programming For Mac OS X" book by Arron Hiilegass, I'm getting a little frustrated. I'm able to work the examples, but he doesn't explain exactly why he's using a class in that way (NSNumber instead of int or double for example).

I'm looking for a good book/books to lean Objective-C first. My question is, what would be a good book? I'm looking at "Programming In Objective-C 2.0" and it looks pretty decent. Where would I go from there? I'm thinking I should then re-start on my Cocoa book.

Also, are there any resources on the internet that would help in the transition from PHP to Objective-C? I know PHP is a loosely-typed scripting language, so it has its differences. There are some things that just don't make sense with Obj-C and Cocoa, why can't I put integers into an NSMutableArray?

Anyways. Thanks for the help! (I'm only 14, so go easy on me if I made any mistakes in my Q. )

Answers


I've just gone through "Programming In Objective-C 2.0" myself, and it's pretty good. I'd recommend it, especially if you've never used C (or if you've forgotten it, like me).

However, Apple really has excellent documentation. If you don't mind reading online, I'd start with their Getting Started with Cocoa page.


I'm able to work the examples, but he doesn't explain exactly why he's using a class in that way (NSNumber instead of int or double for example)...

There are some things that just don't make sense with Obj-C and Cocoa, why can't I put integers into an NSMutableArray?

NSNumber is a much more useful type than a primitive type like int or double, as it is often used in conjunction with other objects you'll run into as you program in Cocoa.

For example, in order to package a number as a value into a resizable array (like an NSMutableArray) or an associative array (an instance of NSDictionary), you need to turn the number primitive (int, double, etc.) into a serializable, or archivable object — an NSNumber.

Primitives can't be serialized, unlike an NSNumber, because primitives aren't in the basic set of "Core Foundation" types (NSNumber, NSArray, NSString, etc.) that Apple has worked hard to make available to you.

Also, by using NSNumber you also get a lot of bonus convenience methods for free: you can quickly convert the number into a string, for example, by simply typing [myNumber stringValue].

Or, if you're treating your NSNumber as the price of something ("$1.23"), you can apply an NSNumberFormatter to make sure that operations on the number give results that have the format that you would expect (e.g. if you add two price values, you would expect to get a currency value in return).

That's not to say you can't or shouldn't use int or double variables. But in many cases, you'll find an NSNumber is a better option, in that you can write less code and get a lot of functionality for "free".


In my experience, I've found the Internet docs to be helpful enough in learning Obj-C and CocoaTouch. My progression went something like this:

1) Watch the Apple Dev videos on iTunes (they're free).

2) Read Getting Started, iPhone Application Programming Guide.

3) Read OOP In Obj-C.

4) Read more: Cocoa Fundamentals, Objective-C Primer, Cocoa Practices.

5) Do some simple tutorials.

IMO: All the info you need is on the Apple Dev:iPhone site. Save your money and don't buy books. If you don't understand "WHY" something is done in one of the guides or tutorials, cross reference it immediately with other sources from Google.

You've gotta keep in mind that the learning curve here is pretty big. It was for me and I'm studying things like this every day in college. So, stick with it and read in a smart way (skim stuff you know). What I see in myself, is if I understand how the iPhone works and know the flow of data, then programming for it is mainly a syntax problem.

PHP is very different from Objective-C. Furthermore, the way programming problems are solved in PHP in the context of the Internet is very different from the way programming problems are solved in Obj-C, in the context of the iPhone. Because of this, you want to approach the iPhone from a new perspective, and as a student/learner. Take your time and focus on Object Oriented Programming and best practices. This will bless you for years to come.

-Buffalo


Also, to answer your questions:

Objective-C is an object oriented language built as an extension on top of C.

As such it provides both primitive types (like int and double) and objects.

NSNumber is an Objective-C class that represents a number PLUS a number of operations on that number (methods). The advantage of using an NSNumber over a numeric primitive type is that it can be used in an object-oriented fashion (you can send messages to it, you can extent its functionality with "protocols", you can inherit from it, you can pass it in a method that expects an object etc).

As for NSMutableArray, this is a class that provides array-like functionality. It is designed to work with Objective-C object types (it is a container of NSObject type objects and objects that inherit from them) and this is the reason why it cannot contain an integer. It can, however, contain an NSInteger which is an objective-C class that represents an integer.


I would personally try to first learn Objective-C from the apple references at http://developer.apple.com/library/mac/navigation/ and going on to cocoa from there.


I was in a similar boat (PHP moving to Objective-C) and I found the best process was to jump into a project. I went through the Hillegass book as well and it was a good start but the only way to get to know a language is to just muddle through with a clear objective.

It's painful but it works. Coffee, APIs, and kleenex.

EDIT: I just read the last part of your post and saw you were 14. Perhaps switch the coffee with Coke:)


My favorite Objective-C tutorial if you're coming from anywhere at all is the iTunesU course from Stanford on iOS programming. The professor really puts things in perspective from the other books and online tutorials.


Need Your Help

Is it sensible to connect a desktop client directly to MySQL?

mysql client-server

I'm writing a Java desktop client application that retrieves data from a remote MySQL server. For development purposes I have had it connecting directly to the MySQL server (i.e. with DriverManager.

Indication bar in iframe loading inconsistently in IE.

javascript internet-explorer iframe progress-bar

I am using the code structure below for the presentation, which is a jsp page. Usually, the web page takes more than 3 seconds to load completely.