Search for a string in Enum and return the Enum
check out System.Enum.Parse: enum Colors {Red, Green, Blue} // your code: Colors color = (Colors)System.Enum.Parse(typeof(Colors), “Green”);
check out System.Enum.Parse: enum Colors {Red, Green, Blue} // your code: Colors color = (Colors)System.Enum.Parse(typeof(Colors), “Green”);
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 (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
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
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
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
This question suits one of my old notes. I hope this illustration helps:
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
As of Xcode 7 beta 5 (Swift version 2) you can now print type names and enum cases by default using print(_:), or convert to String using String‘s init(_:) initializer or string interpolation syntax. So for your example: enum City: Int { case Melbourne = 1, Chelyabinsk, Bursa } let city = City.Melbourne print(city) // … Read more
For those lured here by title: yes, you can define your own methods in your enum. If you are wondering how to invoke such non-static method, you do it same way as with any other non-static method – you invoke it on instance of type which defines or inherits that method. In case of enums … Read more