views:

1918

answers:

6

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

+3  A: 

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.

JW
Apple's "The Objective-C Programming Language" linked from there completely describes Objective-C and includes examples of what it's discussing. A little dry, but very informative.
Chuck
Matt Egan
A: 

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.

foljs
Thanks! I was working on a simple Foundation Tool to calculate the Mean Absolute Deviation. I wasn't aware there was an NSInteger, I think my documentation downloading was interrupted, so I may only have half. Thanks!
Matt Egan
NSInteger is not a class. It is an alias for an integer of size appropriate to your environment (32-bit vs 64-bit). The following can be found in NSObjCRuntime.h on line 106:#if __LP64__ || NS_BUILD_32_LIKE_64typedef long NSInteger;typedef unsigned long NSUInteger;#elsetypedef int NSInteger;typedef unsigned int NSUInteger;#endif
Martin Gordon
Oops, Martin is right. NSInteger is just a typedef for integer, supposed to a transparent way to transition to 64-bit. You'll have to wrap it in an NSNumber to put it in a collection class.
foljs
+1  A: 

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

Alex Reynolds
Thanks for the explanation! I'm pretty sure there isn't, but is there a fast and easy way to create a new NSNumber object? Also, I know that NSNumber isn't mutable, is there a version that is? Maybe NSInteger? (I'm re-downloading the Core Reference Library right now)
Matt Egan
NSNumber *myNumber = [NSNumber numberWithInt:5];
Alex Reynolds
This is not really accurate. Serialization has nothing to do with whether a value can go in an an NSArray. The reason an int can't go in an NSArray is because an int isn't an object, and NSArray only works with objects (indicated by having initWithObjects: and objectAtIndex: take "id" type arguments -- id is different from int). This is the whole reason NSNumber exists -- it provides an object to wrap an int or a float so they can be stored in places that expect objects. I also wouldn't say NSNumber is more useful than int -- try doing math with NSNumber sometime.
Chuck
NSNumber *myNumber = [NSNumber numberWithFloat:9.876];
Alex Reynolds
Chuck, sorry if that's not clear, but if you want to be able to serialize an NSArray, it has to contain objects that are themselves serializable. As the documentation points out: "Arbitrary objects cannot be serialized. Only instances of NSArray, NSDictionary, NSString, NSDate, NSNumber, and NSData (and some of their subclasses) can be serialized. The contents of array and dictionary objects must also contain only objects of these few classes."
Alex Reynolds
Oops, correcting myself: objectAtIndex: actually *does* take an int argument (the index). I meant to say it *returns* an id (the object).
Chuck
You can put any object into an array. I haven't said that you can't. What I am saying, however, is if you want to be able to serialize an array, you will need to use NSNumber instead of int, double, float, etc. So it's useful to know about NSNumber and how to use it. That's all.
Alex Reynolds
@Alex: That's true. But the statement "To package a number as a value [into an array], you need to turn the number into a serializable ... object" implies that being serializable is the significant difference. Non-serializable objects can go into an array.
Chuck
I guess it's not understandable to me why you would want to put a non-serializable object representation of a number into an array, when NSNumber is already available and serializable.
Alex Reynolds
A: 

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

Paulo
oh god, I love coke.
Matt Egan
Don't forget Ibuprofen. Especially when you get to Quartz drawing.
willc2
A: 

Hey man,

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

Buffalo
A: 

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.

Logan Buth