Methods inside enum in C#

You can write extension methods for enum types: enum Stuff { Thing1, Thing2 } static class StuffMethods { public static String GetString(this Stuff s1) { switch (s1) { case Stuff.Thing1: return “Yeah!”; case Stuff.Thing2: return “Okay!”; default: return “What?!”; } } } class Program { static void Main(string[] args) { Stuff thing = Stuff.Thing1; String … Read more

for each loop in Objective-C for accessing NSMutable dictionary

for (NSString* key in xyz) { id value = xyz[key]; // do stuff } This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary is one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumeration in the … Read more

Collection was modified; enumeration operation may not execute – why?

From the IEnumerable documentation: An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined. I believe the reasoning for this decision is that it cannot be guaranteed that all types … Read more

How to use “enumerateChildNodesWithName” with Swift in SpriteKit?

For now, don’t trust autocomplete to insert the code you need — it drops in signatures from the “header”, but a block signature is not the same as the declaration you need when inserting your own closure for a block parameter. The formal way to write a closure would be to replicate the signature inside … Read more

Intelligent way of removing items from a List while enumerating in C#

The best solution is usually to use the RemoveAll() method: myList.RemoveAll(x => x.SomeProp == “SomeValue”); Or, if you need certain elements removed: MyListType[] elems = new[] { elem1, elem2 }; myList.RemoveAll(x => elems.Contains(x)); This assume that your loop is solely intended for removal purposes, of course. If you do need to additional processing, then the … Read more

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

The stop argument to the Block allows you to stop the enumeration prematurely. It’s the equivalent of break from a normal for loop. You can ignore it if you want to go through every object in the array. for( id obj in arr ){ if( [obj isContagious] ){ break; // Stop enumerating } if( ![obj … Read more

tech