I use Stanford POStagger and I want to define an enum as bellow :
public enum POSType {
CC("Conjunction"),
CD("Number"),
! ("Punctuation")
......
}
How can I define a punctuation as an enum variable?
Related
I have 2 enums.
First enum contains Constants:
enum Constants {
static let settings = "settings"
static let help = "Help"
}
Second Enum is of type String and contains few cases:
enum SettingsName: String {
case Settings = "Settings"
case Help = "Help"
}
How can I associate the String from the Constants enum to my second Enum ?
I've tried case Settings = Constants.settings but is not working.
You can't. The raw value of a case must be a literal.
How to get the raw value from a enum passing the key value? Must work for any enum types working like an extension of enum types.
Any mirror reflection, mappable or RawRepresentable solutions are welcome.
I would like something like this:
enum Test : String {
case One = "1"
}
Test.rawValueFromKey(keyValue: "One") // Must return "1"
// I don't want the solution below, I must get the rawValue through the key name as String.
Test.One.rawValue
I do need to get the rawValue passing the name of the key as a String. Otherwise, I will need to make a switch or many if/else conditions. I have a big Enum and I don't want to check the string passed in a switch for example and pass Test.One.rawValue. I would like to get the rawValue directly through the key as String, just like in a dictionary.
I also don't want the code below, because I have a Enum of 40 items and I think it is ugly to make a switch of 40 items.
switch keyValue {
case "One":
return Test.One.rawValue
break
}
I want something like this:
func rawValueFromKey (keyValue: String) -> Test {
// code here to get the rawValue through the keyValue as String
// return the proper Test Enum
}
I tried some possible solutions using Mirror reflection and enum iterations to find the rawValue through the keyValue but didn't work.
Please give the solution in both Swift 2 and 3.
Thanks
As far as I know, you can't reflect on types so I think you will be able to achieve this only with CustomReflectable or maybe using localized strings for mapping the values/keys. Invariably you'll have to map somehow.
Why not something like this? Sure you are just comparing Strings so it's potentially unsafe (if your enum would conform to CustomStringConvertible, etc), but you can't really avoid that.
I think CaseIterable is available only from Swift 4 though...
protocol KeyRepresentable: CaseIterable {
}
extension KeyRepresentable {
static func fromKey(key: String) -> Self? {
return Self
.allCases
.first { "\($0)" == key }
}
}
Usage:
enum Option: Int, KeyRepresentable {
case a = 1, b = 2
}
print(Option.fromKey(key: "a")?.rawValue)
I have an enum of type String which needs to have multiple variables that have the same value. My enum looks something like this:
class MyClass {
enum MyEnum: String {
case blahA = "blaha"
case blahB = "blahb"
...
static var blahD = "blah"
static var blahE = "blah"
}
}
The reason why I'm using static var's in the above construction is because both "blahD" and "blahE" need to reference the same String value, used in different places (don't ask me why, it just has to be this way). However, I have a method where I need to pass in the value of the enum as follows:
if let testString = myString(foo: MyEnum.blahD) {...}
I unfortunately am getting the following compilation error:
Cannot convert value of type "String" to expected argument type "MyClass.MyEnum".
How do I get around passing the above variable which has duplicate values in my enum in the method, but cast it to the type of "MyClass.MyEnum"?
You can do this if you make the extra case reference the other enum case directly instead of just assigning them the same string value:
class MyClass {
enum MyEnum: String {
case blahA = "blaha"
case blahB = "blahb"
...
case blahD = "blah"
static var blahE = MyEnum.blahD
}
}
Then you can pass MyEnum.blahE the same way you would pass MyEnum.blahD
If the function takes a value of type MyEnum, you cannot do this. The type properties blahD and blahE are simply not of that type. Only the cases of the enum are of type MyEnum.
The function parameter's type must be changed to String.
The only other way around it would be to add a case to the enum that has a raw value matching the value of those two properties: case blahDOrE = "blah". Then you could construct that case: MyEnum(rawValue: MyEnum.blahD), but I can't see that being very useful.
I'm trying to create an enumeration that accepts a generic type as associated value.
The compiler complains:
Reference to generic type 'GenericItem' requires arguments in <...>
The scheme is pretty simple:
struct GenericItem <Item:FormattableAsStringWithPrecision> {
let value: Item
}
enum Enumeration {
case Generic(values: [GenericItem])
}
I can't understand how to make this possible.
You need to add the Generic type to the enum too, the types can be inferred from the initialiser so you do not need to pass it as a generic type argument.
Below is an example of how you might do it.
struct GenericItem<T: CustomDebugStringConvertible> {
let value: T
}
enum Enumeration<T: CustomDebugStringConvertible> {
case Generic(value: [GenericItem<T>])
}
let someValue = Enumeration.Generic(value: [ GenericItem(value: "") ])
edit: I changed the FormattableAsStringWithPrecision to CustomDebugStringConvertible as I assumed it was one of your own custom protocols which can be easily swapped out, but the same logic would still apply for any protocol.
There's a enum like this:
enum Result<Value> {
case Success(Value)
case Failure(NSData?, ErrorType)
}
What's the <Value> part? It's not explained in the Apple documentation.
It's a generic, here is the doc references
What is means is when you create a variable of Result you pass the type in < > brackets
e.g.
var res: Result<String>
Now your Value becomes String
And if your want that to be Int then you will probably do like this
var res: Result<Int>
It's not written in the enum part of the Swift documentation, it's in generics:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html#//apple_ref/doc/uid/TP40014097-CH26-ID179