Implementing -stringByReplacing... as "return [[self retain] autorelease];" makes the same guarantee, that the object returned by -stringByReplacing... will remain valid if the original string is released.

Yes, but there's no guarantee which technique -stringByReplacing.. will use internally.

True, but the MM rules *do* guarantee that -stringByReplacing...* will return an object that remains valid throughout the caller's scope.

Ah, you're right. I reread the memory management documentation and it is guaranteed:

"A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. Exceptions include..."

None of the listed exceptions apply to our string replacement example.

I feel silly getting something so fundamental wrong, though in my defense I feel like Apple must have added that guarantee at some point over the years. A good idea to be sure.

* Is anyone else wishing that we'd chosen a shorter method name to have this discussion? :-)

We could have done worse :P

~Martin

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to