PDA

View Full Version : Where to begin learning Obj-C?




Ajmbc
Jul 12, 2006, 12:24 PM
Hey everyone :)

I'm trying to teach myself Objective-C using the wikibook (http://en.wikibooks.org/wiki/Programming_Mac_OSX_with_Cocoa_for_beginners) I found in a thread about a week ago.

Is this a good place to learn some of the language if I have minimal C++ experience? Where would you refer me to online?

One other thing I found there was particularly confusing. Why is there an asterisk after some class names when referencing instances? (for instance NSString* myString)

Thanks in advance :)
-Alex



ham_man
Jul 12, 2006, 12:45 PM
1) I would get Cocoa Programming for Mac OS X by Aaron Hillegrass (http://www.amazon.com/exec/obidos/ASIN/0321213149/bignerdranch-20/104-7659260-8627965). It's a fantastic book and well worth the $30.

2) Doesn't NSString* mystring intilize an object mystring of class NSString...? Or am just I rusty?

Josh
Jul 12, 2006, 12:52 PM
1) I would get Cocoa Programming for Mac OS X by Aaron Hillegrass (http://www.amazon.com/exec/obidos/ASIN/0321213149/bignerdranch-20/104-7659260-8627965). It's a fantastic book and well worth the $30.


I second that. I'm currently going through the book, and really enjoy it.

I started with basic C/C++ knowledge before hand, and while somethings give me a little trouble, it makes sense once it's completed and I can go through it again.

Aaron Hillegass's approach is more straight-forward, without much explanation on *why* with more emphasis on *how*.

The concepts aren't hard to grasp for an entry level C++ programmer, but the new obj-c/cocoa additions can be tricky to get the hang of at first (for me, anyway).

whooleytoo
Jul 12, 2006, 12:54 PM
Before learning Obj-C, you should ideally have a good knowledge of C, and know the meaning and purpose of variables, functions, pointers, etc. Knowing C++ isn't at all necessary, although knowing the logic behind object oriented programming isn't a bad idea.

If you Google for "learn C online", you'll find plenty options, I can't really recommend one over the other.

As for your question:

NSString stringObject - creates an instance of an NSString object, but it's never done this way, I don't know if the compiler will even let you do this.

NSString* stringObject - this creates a pointer; a variable for holding the address of an NSString object.

The asterisk also can be used when de-referencing a pointer, but I don't think you'll be finding much use for that in Obj-C.

Monkaaay
Jul 12, 2006, 12:58 PM
I just started that book as well. So far it's been quite good but you should really pick up the "buddy book", Programming in Objective-C (http://www.amazon.com/gp/product/0672325861/ref=pd_bxgy_img_b/102-3091348-5862525?ie=UTF8).

HiRez
Jul 12, 2006, 04:09 PM
Because I'm lazy I'm gonna repost a snippet of a couple of my old posts:
--------------------------------------------------

Once you know the basics of C, I would continue into Objective C before starting with Cocoa, by reading Apple's Objective-C Programming Language (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html#//apple_ref/doc/uid/TP30001163) and Programming in Objective-C (http://www.amazon.com/gp/product/0672325861/qid=1143893107/sr=2-1/ref=pd_bbs_b_2_1/103-8462982-0472631?s=books&v=glance&n=283155) by Stephan Kochan. Then you can move on to Apple's Cocoa documentation, most of which can be accessed directly from Xcode, and also check out O'Reilly's Mac DevCenter (http://www.macdevcenter.com/), Cocoa Dev Central (http://cocoadevcentral.com/), Cocoabuilder (http://www.cocoabuilder.com/archive/bydate), CocoaDev (http://www.cocoadev.com/), the Apple Cocoa Mailing List (http://search.lists.apple.com/?q=&cmd=Search%21&form=extended&m=all&ps=10&fmt=long&wm=wrd&wf=2221&sp=1&ul=cocoa-dev), Wil Shipley's Blog (http://wilshipley.com/blog/), and these Cocoa Bindings tutorials (http://homepage.mac.com/mmalc/CocoaExamples/controllers.html). There are a number of decent books on Cocoa specifically, but I would recommend starting with Cocoa Programming for Mac OS X (http://www.amazon.com/gp/product/0321213149/ref=pd_bbs_null_1/103-8462982-0472631?s=books&v=glance&n=283155) by Aaron Hillegass.

---------------

One more Cocoa book I go to again and again is Cocoa Programming (http://www.amazon.com/exec/obidos/tg/detail/-/0672322307/ref=cm_bg_d_2/104-1942456-3494313?v=glance). This is more of a reference book for Cocoa than a tutorial but it's huge and comprehensive, with lots of nice code examples. The only problem is it hasn't been updated in years so much of the information is now out of date (and there is no discussion of any of the API introduced in the last few OS X versions). I believe it covers everything through 10.2. But, most of the core API is still the same and this book continues to be useful to me.

Catfish_Man
Jul 12, 2006, 05:14 PM
NSString stringObject - creates an instance of an NSString object, but it's never done this way, I don't know if the compiler will even let you do this.

This used to be legal objc, but not anytime recently. iirc the Gnu runtime still allows it.

slooksterPSV
Jul 12, 2006, 10:18 PM
Online? I refer you to an online store to buy these books:
Cocoa Programming for Mac OS X by Aaron Hillegass
Programming in Objective-C by Steve Kochan

mindwalkernine
Jul 23, 2006, 06:59 PM
Is this a good place to learn some of the language if I have minimal C++ experience? Where would you refer me to online?

Online is really a nightmare (been that route over the last year), unless you just have a quick question. If you have a quick question, a Cocoa forum is a great place to go. Check out Apples cocoa lists. Subscribe. But you don't aways get an answer. Everyone seems to be talking about advanced stuff.

Get Steve Kochans book. It teaches you Objective-C and gives a reference to the C too. It takes you through the foundation but leaves out networking and a few other interesting topics, but it's a great place to start. I've checked the bookshelves and online. There really isn't anything that out does what his book does for beginners. One of it's down sides is that once you get through it, it's not the best reference, because Kochan's examples are progressive. I.e., let's say you read the whole book and started writing programs, but you needed to review your memory management. So you turn to where he talks about memory management. You look at his explanation. His explanation requires that you look back to previous chapters to understand the code example in that chapter. Although, as you get better at understanding the language, assumptions can help you with that problem, but this is really not a good way for beginners, because you spend time on his personal implementation rather than on the concept you are trying to reference.

Once you get familiar with Objective-C, then Cocoa is your next step. Aarons book has the best examples to get you going. "Cocoa Programming for Mac OS X by Aaron Hillegrass" Although, Interface Builder can confuse you because it hides alot of stuff you learn in Objective-C, so when you get to Interface Builder using Cocoa (All Cocoa books use Interface Builder) you'll have to really think about what is happening, because it's easy to get lost with what it hides from you.

The asterik is used in several places:

In your class interface:

@interface MyClass : NSObject
{
NSArray *myArray;
}
@end

The asterisk here represents myArray as a pointer to an instance of an NSArray object.

It is also used when you declare methods:

- (NSNumber *)myNumber;

In the -myNumber method, it returns a pointer to an instance of NSNumber class.

It is also used in methods that take arguments:

- (void)setMyNumber:(NSNumber *)number;

the number variable is a pointer to an instance of NSNumber class.

If you don't put that asterisk in there, the compiler will complain you are trying to
"statically allocate an instance of Objective-C class `NSNumber'.

kainjow
Jul 24, 2006, 12:48 AM
Ajmbc: if you don't have any background in C/C++, I'd suggest learning that first. Objective-C will be much easier after you learn the basics of C/C++ (especially C++, since Objective-C and C++ are both OO languages).

slooksterPSV
Jul 24, 2006, 08:17 PM
But now get this, NSRect you don't use an asterisk a lot of the time. Some things do, some things don't.

Oh one other thing:
Don't expect to learn programming and program an OS in one day. Oh heck no, expect to learn to program, try stuff out, LEARN IT, (that's key) and then build on as you go.

E.g. make a simple program that is a calculator, it takes two numbers and adds them, next add on to it so that you can multiply divide, etc. (give the user a menu or have them type it in), next build on that and have the program remember the value and store it, then be able to call up that value. Build on that and make the program more complex by allowing sin and cos. Even more complex? how about adding arctan, arcsin, the quadratic formula. Very complex? how about sigma, integration, derivatives. Any more? well there's tons, add on a graphing calculator, etc.

Make your programs simple then expand, look at photoshop, have you compared CS2 to Photoshop version 1.0?

Littleodie914
Jul 24, 2006, 10:52 PM
Yep, also going through the Hillegass book, it's really good. The biggest part in learning a new language isn't reading the book, but instead coding for yourself the examples it shows you and straying from the line a bit to modify them.

For example, when working with classes, instead of creating a LotteryEntry with two ints and an NSCalendarDate, make yourself a class called CollegeBummers and give it a couple different variables, like NSStrings to hold the teacher name, class name, etc. and maybe an NSArray 5 values long that holds booleans to determine what days of the week you have the class.

Makes it a lot more fun, and helps you actually think about the code and what you plan on doing with it, rather than following step-by-step and semi-understanding why you're doing what you're doing. Good luck! :cool:

caveman_uk
Jul 25, 2006, 02:53 AM
But now get this, NSRect you don't use an asterisk a lot of the time. Some things do, some things don't.

That's because it's a tiny little struct. Most other things you use most of the time in objective-C are classes - NSRect isn't.

HiRez
Jul 25, 2006, 04:11 PM
That's because it's a tiny little struct. Most other things you use most of the time in objective-C are classes - NSRect isn't.This threw me off for a long time as well when I was learning Cocoa. Sure, once you know what a struct is, and know NSRect, NSPoint, etc. are structs, it makes sense, but it's initially confusing and that's one of the few beefs I have with the Hillegass book: it should explain how most "NS" entities are objects, but a few are not, and tell you how to identify and use them.

caveman_uk
Jul 26, 2006, 02:55 AM
And of course being structs they don't use the objective-c 'ways' of accessing data (setters and getters etc). They use the standard C dot nomenclature.

slooksterPSV
Jul 26, 2006, 07:57 AM
And of course being structs they don't use the objective-c 'ways' of accessing data (setters and getters etc). They use the standard C dot nomenclature.
E.g. if I have a struct like this: (what are the naming conventions/recommendations for structs?)

struct myTestStruct
{
int dataLength;
char* data;
bool create;
};

//or should it be my_test_struct

myTestStruct test;
test.dataLength = 0;
test.data = "hi";
test.create = true;

whooleytoo
Jul 26, 2006, 08:46 AM
I think struct types, like classes, tend to be capitalised. Other than that, the naming convention is entirely up to you.

I don't use structs that often, but I seem to recall they're more often declared:


typedef struct
{
.
.
} BananaCheeseCake ;

BananaCheeseCake myCheeseCake = .....