• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Object alloc questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Object alloc questions


  • Subject: Re: Object alloc questions
  • From: eHolley <email@hidden>
  • Date: Wed, 25 Feb 2004 13:20:01 -0700

James,

Hi -- I am very new to Objective-C, slowly working my way through both Learning Cocoa with Objective-C and Cocoa Applications: A Step-by-Step Guide. I hope it's okay to post newbie questions here -- if not, please send me off to the appropriate mailing list/forum!

This is the right place.

Here's a couple questions:

I understand the idea of alloc-ing and init-ing an object, particularly in one statement such as:

NSObject * myObject = [[NSObject alloc] init];

What I don't understand is how the same function is performed here:

NSString * aString = [@"How does this initialize the object?"];

There is a behind-the-scences allococation going on here. The Objective-C runtime knows that on Darwin (Mac OS X), literal strings (as indicated by @) are to be created using NSString. (Also, lose the brackets).

I also get, sort of, some of the initWithThisString calls, which are essentially init calls overriding the default init for the class?

A class may have many initializers for varied purposes. The alloc method prepares the memory for your object, but does no more. You object will not likely be in a usable state after a lonely alloc call. Calling one of the init methods initializes your object and prepares it for use. Its safe and appropriate to call the any of the init methods provided by a class. The default init method doesn't really do much.

I guess the question is -- when do I use the alloc/init method and when do I assign the object it's terms immediately?

You'll use alloc/init when creating the object and preparing for use. Assigning its terms immediately? What do you mean?

Is it a matter of whether you know what the object is to be assigned or not at initialization?

No.

Can any object be initialize via method 2 above?

No. Using @ is specific to NSString for Darwin Objective-C.

Or is this special to NSString/NSArray objects (if figured not, since ABAddressBook can take the same calls...)

Also, I've not been able to find a clear explanation as to what the * does - what is it telling the compiler?

The * is when used with the name of an object variable indicates that the variable is a pointer.

NSString myString;

This won't compile. Objects must be created on the heap and are referenced via a pointer. Hence, *.

I don't mean to assume your understanding of C, but pointers are a basic, although complex, concept. A good C book would help explain the C fundamentals you'll need to understand Objective-C.

Essentially, a pointer is an address to something else. Anything. Much like if you were to write the address of your home on letters. The address takes up some space on the letter, but certainly not the amount that your house would should you find a creative way to affix it to a 9"x4" envelope. The address simply lets the post office know where to find you. It points to your house. You can pass pointers to your house all over, but have only one house. Sending the actual house along whenever someone asked you for your address would be financially limiting.

Objects in Objective-C are created on the heap and are accessed through pointers. If your working from a 32-bit operating system, the pointer may be 32 bits wide. It holds the memory address of where the object lives.

Although all objects are declared using pointers, declaring a variable with the type "id" means, among other things, you don't need to use an asterisk (it's built in). For instance, these two lines produce identical results:

id myString = @"my string";
NSString * myString =@"my string";

-Erik


I've got lots of other questions, but I'll wait to see if this is the right place for them...

Thanks!


James
_______________________________________________
objc-language mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/objc-language
Do not post admin requests to the list. They will be ignored.
_______________________________________________
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.


  • Prev by Date: Re: NSArrayController and bindings
  • Next by Date: Re: ObjC method syntax (was: Re: cocoa-dev digest, Vol 2 #3738 - 11 msgs)
  • Previous by thread: Re: 1 sheet to kill all sheets?
  • Next by thread: toolbar question
  • Index(es):
    • Date
    • Thread