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

Re: Mutable Copying


  • Subject: Re: Mutable Copying
  • From: The Amazing Llama <email@hidden>
  • Date: Tue, 12 Aug 2003 21:36:04 -0700

On Tuesday, August 12, 2003, at 08:49 PM, Francisco Tolmasky wrote:
Can someone explain to me the difference in these two examples:

1.
NSMutableArray *array= [[NSMutableArray alloc] init];
//... changes to array
NSMutableArray *array2= [array copy];

This is wrong.

2.
NSMutableArray *array= [[NSMutableArray alloc] init];
//... changes to array
NSMutableArray *array2= [array mutableCopy];

This is right.

The first one will actually set array2 to be an NSArray, not an NSMutableArray. -copy is defined to give you an immutable copy of the object if such a thing exists. An immutable NSMutableArray is an NSArray.

Note, however, that since -copy returns an id (or maybe it's an NSObject), this is not a compile-time error. It might not even be a runtime error if you never send array2 any mutating messages. However, it's a little misleading to write the code.

Seth A. Roby The Amazing Llama < mail or AIM me at tallama at mac dot com>
"Life is like an exploded clown. It's really funny until you figure out what just happened."
_______________________________________________
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.
References: 
 >Mutable Copying (From: Francisco Tolmasky <email@hidden>)

  • Prev by Date: Mutable Copying
  • Next by Date: Re: ScreenSaver with a subview won't preview
  • Previous by thread: Mutable Copying
  • Next by thread: Cocoa - multithread safe?
  • Index(es):
    • Date
    • Thread