• 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: Attributed Strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Attributed Strings


  • Subject: Re: Attributed Strings
  • From: Sherm Pendley <email@hidden>
  • Date: Fri, 11 Mar 2005 17:24:22 -0500

On Mar 11, 2005, at 4:57 PM, Mike R. Manzano wrote:

Regarding NSAttributedStrings, the docs say for its init methods:

Returns an initialized object, which might be different than the original receiver

Does this mean that I should autorelease my original object before initing it? Like this?

NSAttributedString* aString = [ [ NSAttributedString alloc ] autorelease ] ;
NSAttributedString* myActualString = [ [ aString initWithString:@"la dee dah" ] retain ] ;

No, that's not what it means. It means you have to "chain" the +alloc and -init together, like this:


NSAttributedString *aString = [[NSAttributedString alloc] init];

You can't do it in two separate calls, like this:

// ** incorrect code **
NSAttributedString *aString = [NSAttributedString alloc];
[aString init];

Because aString will no longer refer to the correct object; it will refer to the original object returned by +alloc, not the one returned by -init.

No special care needs to be taken with regards to memory management. If -init needs to return a different object, it will automatically release the original and retain the new one. So the normal rules - i.e. you've created an object with +alloc, so you're responsible for releasing it later - still apply as always.

The same thing is generally true of any +alloc/-init pair, incidentally. It's *always* possible, and quite common for classes that are members of a class cluster, for -init to return a different object than the one it was called on. Why the document writers thought that was worth special mention in NSAttributedString in particular, I don't know - it's certainly not unique in that respect.

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Attributed Strings (From: "Mike R. Manzano" <email@hidden>)

  • Prev by Date: Re: Is Mac Mini capable to develop cocoa app?
  • Next by Date: Re: Is Mac Mini capable to develop cocoa app?
  • Previous by thread: Attributed Strings
  • Next by thread: Avoiding flicker when dropping in a view
  • Index(es):
    • Date
    • Thread