enumeration

Objective-C 2.0 and Fast Enumeration throwing exceptions

给你一囗甜甜゛ 提交于 2019-11-30 21:36:12
I have a block of code which is similar to the following: for (NSDictionary *tmp in aCollection) { if ([[bar valueForKey:@"id"] isEqualToString:[tmp valueForKey:@"id"]]) { break; } else { [aCollection addObject:bar]; } } Is this technically an exception in Objective-C 2.0? It appears you cannot mutate a collection with fast enumeration. This is the result of an error: *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <NSCFArray: 0x396000> was mutated while being enumerated.' What's the best way to solve this? Well the way to solve it is not to mutate

Iterating through an Enum in Swift 3.0

≯℡__Kan透↙ 提交于 2019-11-30 20:58:44
I have a simple enum that I would like to iterate over. For this purpose, I've adopted Sequence and IteratorProtocol as shown in the code below. BTW, this can be copy/pasted to a Playground in Xcode 8. import UIKit enum Sections: Int { case Section0 = 0 case Section1 case Section2 } extension Sections : Sequence { func makeIterator() -> SectionsGenerator { return SectionsGenerator() } struct SectionsGenerator: IteratorProtocol { var currentSection = 0 mutating func next() -> Sections? { guard let item = Sections(rawValue:currentSection) else { return nil } currentSection += 1 return item } } }

C# Class is IEnumerable AND an IEnumerator at the same time. What are the issues with this?

家住魔仙堡 提交于 2019-11-30 20:03:31
I have a class called GenericPermutations that is both enumerable and an enumerator. Its job is to take an ordered list of objects and iterate through each permutation of them in order. Example, an integer implemenation of this class could iterate through the following: GenericPermutations<int> p = new GenericPermutations<int>({ 1, 2, 3 }); p.nextPermutation(); // 123 p.nextPermutation(); // 132 p.nextPermutation(); // 213 // etc. So its enumerable in the sense that it contains a 'list' of things you can enumerate over. It's also an enumerator, because its job involves finding the next

Using Scala 2.10 reflection how can I list the values of Enumeration?

被刻印的时光 ゝ 提交于 2019-11-30 19:22:56
Having a following enumeration object ResponseType extends Enumeration { val Listing, Album = Value } How do I get a list of its vals? If you want to be thorough about this, you need to check that your symbols have Value as a supertype: def valueSymbols[E <: Enumeration: TypeTag] = { val valueType = typeOf[E#Value] typeOf[E].members.filter(sym => !sym.isMethod && sym.typeSignature.baseType(valueType.typeSymbol) =:= valueType ) } Now even if you have the following (which is perfectly legal): object ResponseType extends Enumeration { val Listing, Album = Value val someNonValueThing = "You don't

Need to make an enumeration of type double in c#

谁说我不能喝 提交于 2019-11-30 19:19:01
问题 How can I create an enum of type double? Is it possible or do I have to create some kind of collection and hash? 回答1: You can't make it an enumeration. http://msdn.microsoft.com/en-us/library/y94acxy2.aspx One of possibilities: public static class MyPseudoEnum { public static readonly double FirstVal = 0.3; public static readonly double SecondVal = 0.5; } 回答2: I guess it depends on how much trouble you want to go to. You can convert a double to a long, so if you converted your double values

Strange “Collection was modified after the enumerator was instantiated” exception

自古美人都是妖i 提交于 2019-11-30 17:44:01
Perhaps someone can point me in the correct direction, because I'm completely stumped on this. I have a function that simply prints out a LinkedList of classes: LinkedList<Component> components = new LinkedList<Component>(); ... private void PrintComponentList() { Console.WriteLine("---Component List: " + components.Count + " entries---"); foreach (Component c in components) { Console.WriteLine(c); } Console.WriteLine("------"); } The Component object actually has a custom ToString() call as such: int Id; ... public override String ToString() { return GetType() + ": " + Id; } This function

Treat Enumeration<T> as Iterator<T>

假装没事ソ 提交于 2019-11-30 17:28:22
I have a class that implements the Enumeration<T> interface, but Java's foreach loop requires the Iterator<T> interface. Is there an Enumeration to Iterator Adapter in Java's standard library? Bozho You need a so called "Adapter", to adapt the Enumeration to the otherwise incompatible Iterator . Apache commons-collections has EnumerationIterator . The usage is: Iterator iterator = new EnumerationIterator(enumeration); If you just want something to iterate over in a for-each loop (so an Iterable and not only an Iterator), there's always java.util.Collections.list(Enumeration<T> e) (without

Iterating through an Enum in Swift 3.0

放肆的年华 提交于 2019-11-30 17:11:11
问题 I have a simple enum that I would like to iterate over. For this purpose, I've adopted Sequence and IteratorProtocol as shown in the code below. BTW, this can be copy/pasted to a Playground in Xcode 8. import UIKit enum Sections: Int { case Section0 = 0 case Section1 case Section2 } extension Sections : Sequence { func makeIterator() -> SectionsGenerator { return SectionsGenerator() } struct SectionsGenerator: IteratorProtocol { var currentSection = 0 mutating func next() -> Sections? { guard

Objective-C 2.0 and Fast Enumeration throwing exceptions

淺唱寂寞╮ 提交于 2019-11-30 17:05:44
问题 I have a block of code which is similar to the following: for (NSDictionary *tmp in aCollection) { if ([[bar valueForKey:@"id"] isEqualToString:[tmp valueForKey:@"id"]]) { break; } else { [aCollection addObject:bar]; } } Is this technically an exception in Objective-C 2.0? It appears you cannot mutate a collection with fast enumeration. This is the result of an error: *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <NSCFArray: 0x396000> was mutated

Java: Enumeration from Set<String>

蓝咒 提交于 2019-11-30 16:47:01
I have a simple collections question. I have a Set<String> object. I want an enumeration of the strings in that set. What is the cleanest/best way to go about it? EDIT: There's no need to write your own (although I'll leave the implementation below for posterity) - see Kevin Bourrillion's answer for the one in the JDK. If you really need an enumeration, could could use: Enumeration<String> x = new Vector(set).elements(); It would be better to use Iterable<E> if at all possible though... A better alternative is to write a small wrapper class around Iterator<E> . That way you don't have to take