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

Re: -[NSMutableSet randomObject]


  • Subject: Re: -[NSMutableSet randomObject]
  • From: Jens Alfke <email@hidden>
  • Date: Tue, 25 May 2010 18:38:22 -0700

On May 25, 2010, at 9:49 AM, Michael A. Crawford wrote:

>    // If the array is empty, throw an exception.  The caller should know better.
>    // If there is only one, return object zero every time.
>    // If there are only two, alternate with every access.
>    // If there are three or more, randomize but never provide the same object
>    // twice in a row.

I don’t think you can provide this behavior as a category, because it requires extra state: each call has to remember what object was returned by the previous call. That state is per-object so it would need to live in the object. But categories don’t give you extra instance variables*. Even if you could put state into the object, I don’t think it’s a good design — what if you ended up with two clients that wanted to get random access to the same set and interleaved their calls?

It sounds like what you want is the iterator (aka enumerator) design pattern, where you create a separate lightweight object that returns the “next” item each time it’s called. You can then put the state in the iterator.

—Jens

* I know there are ways of doing this; but I still think it’s a bad idea, as the next sentence explains._______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

References: 
 >-[NSMutableSet randomObject] (From: "Michael A. Crawford" <email@hidden>)

  • Prev by Date: UITabBar as a UISegmentedControl???
  • Next by Date: Re: reformat drive to HFS via API
  • Previous by thread: Re: -[NSMutableSet randomObject]
  • Next by thread: Create an NSImage using pixel (RGB) data
  • Index(es):
    • Date
    • Thread