Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Ajmbc

macrumors regular
Original poster
Jan 29, 2003
232
0
Louisiana
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
 

Josh

macrumors 68000
Mar 4, 2004
1,640
1
State College, PA
ham_man said:
1) I would get Cocoa Programming for Mac OS X by Aaron Hillegrass. 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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
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 and Programming in Objective-C 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, Cocoa Dev Central, Cocoabuilder, CocoaDev, the Apple Cocoa Mailing List, Wil Shipley's Blog, and these Cocoa Bindings tutorials. There are a number of decent books on Cocoa specifically, but I would recommend starting with Cocoa Programming for Mac OS X by Aaron Hillegass.

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

One more Cocoa book I go to again and again is Cocoa Programming. 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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
whooleytoo said:
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

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
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

macrumors newbie
Jul 23, 2006
9
0
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:

Code:
@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:

Code:
- (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:

Code:
- (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

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
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

macrumors 68000
Jun 9, 2004
1,813
8
Rochester, NY
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
slooksterPSV said:
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

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
caveman_uk said:
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.
 

slooksterPSV

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
caveman_uk said:
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?)
Code:
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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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:

Code:
typedef struct
{
.
.
} BananaCheeseCake ;

BananaCheeseCake myCheeseCake = .....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.