Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a function or macro for "oneOf" ... casting in variadics?
    primarykey
    data
    text
    <p>Note - using variadic arguments, <strong>Phix</strong> has provided a super solution below. However it gives compiler warnings when used with integers since you're casting them to an id, and it will not work with for example floats.</p> <p>Question -- how write a facility that chooses one item from a list? Ideally, it would be as flexible as possible <strong>regarding the type</strong> of the items/objects/whatever in the list.</p> <p>Example usage:</p> <pre><code>NSInteger openingMap = [utilities oneOf:1, 2, 3, 7, 8]; initialAngle =oneOf(1.25, 1.75, 1.95, 2.00, 2.01); self.spaceShipNickName =oneOf(@"Blaster",@"Blitzen",@"Stardancer",@"Quantum"); self.chassisProperty = oneOf(titanium, neutronium, unavailablium); [fireworksLayer paintStars:oneOf(blue,green,white) howMany:oneOf(20,25,50)]; [trump chooseDefaultSuite:oneOf(diamonds,hearts,clubs,spades)]; // normally have a few explosions, but, occasionally have a huge display... explosionCount = oneOf( 2,2,2,3,4,1,28,3,3,3,70 ); </code></pre> <p>Note that some examples are integers, some enums, some NSStrings, etc. So again, it would be most impressive if the one facility could handle different types. (Rather than perhaps a related group, like ... oneOfIntegers, oneOfStrings, oneOfObject, whatever.)</p> <p>It goes without saying that to choose the random item, just use...<br> = arcrandom() % number-of-items</p> <p>You could use <strong>an Objective C method or class, a c function, or some sort of system of macros, category extension to NSMutableArray, or indeed blocks - or anything else.</strong> It should be as flexible as possible for broad use anywhere in a typical iOS or Mac project... any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload