Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From PHP to Objective-C [closed]

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. )

like image 568
Matt Egan Avatar asked May 02 '09 00:05

Matt Egan


3 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.

like image 79
JW. Avatar answered Oct 19 '22 23:10

JW.


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".

like image 43
Alex Reynolds Avatar answered Oct 19 '22 23:10

Alex Reynolds


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

like image 36
Buffalo Avatar answered Oct 20 '22 01:10

Buffalo