0

Are Objective-C's (id) methods used for initializing after alloc only?

NSString *stringC = @"My World"; 
NSString *sample = [[NSString alloc] initWithString:stringC];
elitalon
  • 9,191
  • 10
  • 50
  • 86
user1637802
  • 73
  • 1
  • 4

2 Answers2

2

Objective-C's init methods are intended for only newly-alloc'd instances, and here's why:

init methods are allowed to re-assign self and return a different pointer than what the associated alloc did (which is why you always assign your variable as the return of init). This means that calling init on a previously-initialized object may give you back a pointer to new memory, which creates a few problems.

First, if you don't assign the new pointer to your variable, it was pointless or worse. Second, any additional pointers to the "old" object are now "dangling", which will get nasty.

In terms of the @"foo" construct, it's roughly analogous to a const string in C (NSStrings are immutable, btw), but it will be a pointer to an NSString instance instead of a pointer to an in-volatile char, and you don't need to init it. I'm not sure if you left out the asterisk by mistake or not, but @"" does produce a pointer to an NSString, so that line needs the asterisk, just like the next.

note on id

id is Objective-C's special void * for objects It essentially tells the compiler two things:

  1. This is a pointer
  2. The thing being pointed to is an "object"

and #2 there has one quintessential implication: you can send messages to it

So similar to void *, you can get away with passing something type-cast as (id) where some other object-type is required and the compiler won't complain.

Community
  • 1
  • 1
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • 2
    @Thilo quite a lot of the built-in classes since its a classic way to implement class clusters. Calling `alloc` returns a factory that knows how to produce the appropriate instance for any sort of init. I can't check from here but I'll wager `NSNumber` is an example — you'll get a different subclass of NSNumber depending on which init you call. – Tommy Aug 31 '12 at 06:03
0

It's better to say that prior to the init* methods, you should alloc.

For example,

NSString * test = [NSString stringWithFormat:@"hello world"];

also returns an ID but is not after an "alloc" call.

huggie
  • 17,587
  • 27
  • 82
  • 139
  • How would you even call any method before alloc? – Thilo Aug 31 '12 at 05:55
  • I guess I would put it as the difference between message to class or message to object. But you're mostly right. – huggie Aug 31 '12 at 05:57
  • + (id)stringWithString:(NSString *)aString I don't know what the (id) means or the (id) Datatype is – user1637802 Aug 31 '12 at 06:00
  • The ID data type is simply a reference to an Objective-C object. You can think of it as pointer if you're familiar with the concept, except that ID is used to reference specifically Objective-C object, not just any object. – huggie Aug 31 '12 at 06:04
  • See if this helps you. http://unixjunkie.blogspot.tw/2008/03/id-vs-nsobject-vs-id.html – huggie Aug 31 '12 at 06:05