Re: Newbie coming to Cocoa from the world of C++
Re: Newbie coming to Cocoa from the world of C++
- Subject: Re: Newbie coming to Cocoa from the world of C++
- From: Jeff Gilbert <email@hidden>
- Date: Wed, 05 Mar 2003 13:14:10 -0600
Hi Britt,
I hope you enjoy Cocoa/Objective-C. It's a really great way to develop apps.
On Wednesday, March 05, 2003, at 12:30PM, Britt Green <email@hidden> wrote:
>
1) In Obj C, instantiating an object is done like this:
>
NSObject * myObject = [NSObject alloc];
>
>
Is this equal to doing the following C++ command:
>
MyObject* foo = new MyObject();
Yes.
>
2) In C++ one doesn't explicitly call the constructor when an object is
>
created. However, in Obj C one needs to call the init method, correct?
Yes. You'll note that -init returns an id (a generic pointer to an object). It is possible for -init to return a pointer to an object different from the one allocated. Typically, you allocate and construct an Objective-C object with one statement such as:
NSObject* myObject = [[NSObject alloc] init];
>
3) When using the @ sign in front of some quoted text, that
>
automatically converts that text into an NSString?
Yes. You can even use the string literal as the receiver of an NSString message. For example, you can do this:
unsigned int stringLength = [@"Hello world" length];
>
4) What's the difference between an id and a Class?
An id is a generic pointer to an Objective-C object (an instance). It is similar to a void* from C/C++. A Class is a pointer to an objc_class struct which contains class definiton data. It describes the parent class, list of instance variables, list of methods, etc.
>
5) Obj C has two types of methods: class and instance. Are class
>
methods the same as C++'s static methods?
Yes. Class methods (static methods) as denoted with a + before the method name. Instance methods are denoted with a - before the methods name.
For example:
+ (id) stringWithString:(NSString*)aString; // a class method
- (id) initWithString:(NSString*)aString; // an instance method
Jeff Gilbert
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.