Swift Enum Raw Value: Not Working with Cgfloat = -1.0

Swift enum raw value: not working with CGFloat = -1.0

That sounds like a bug. However it seems to work if you omit the decimal part:

enum Aspect : CGFloat {
case Clockwise = 1
case Anticlockwise = -1
}

couldn't set enum type as CGFloat

This error occur because you are not adding UIKit framework in you file just to add UIKit in your file, error will be vanished.

Typecasting in enumeration not working in Swift

This is an enum that has a raw value that is a CGFloat.

What's wrong with the top case is that only a literal number is legal as a raw value. You cannot assign a variable like UIScreen.main.bounds.height. You must write out an actual number, there and then.

Taking a longer view, it looks like what you want here might not be an enum, or might not be an enum that takes a raw value. For example, you can have an enum that has an associated value:

enum CardPosition {
case top(CGFloat)
case middle(CGFloat)
case bottom(CGFloat)
}

Now you can attach the value at initialization time:

let myPosition = CardPosition.top(UIScreen.main.bounds.height)
let myOtherPosition = CardPosition.middle(500)

Note that you cannot mix and match; if we're going to use an associated value, then this enum can't have a fixed raw value.

Enum with raw type cannot have cases with arguments

Swift enum can have either raw values or associated values, but not both at the same time. In your case, case max = 1 is a raw value, while custom(CGFloat) is an associated value.

To overcome this limit, you could use an enum with associated values with a computed property:

enum JPEGCompressionLevel {
case custom(CGFloat)
case max, high, med, low

var value: CGFloat {
switch self {
case .max:
return 1.0
case .high:
return 0.9
case .med:
return 0.5
case .low:
return 0.2
case .custom(let customValue):
return customValue
}
}
}

let a: JPEGCompressionLevel = .custom(0.3)
let b: JPEGCompressionLevel = .max

print(a.value)
print(b.value)

For more information, you can refer to this article.

How to access a Swift enum associated value outside of a switch statement

As others have pointed out, this is now kind of possible in Swift 2:

import CoreGraphics

enum Line {
case Horizontal(CGFloat)
case Vertical(CGFloat)
}

let min = Line.Horizontal(0.0)
let mid = Line.Horizontal(0.5)
let max = Line.Horizontal(1.0)

func doToLine(line: Line) -> CGFloat? {
if case .Horizontal(let value) = line {
return value
}
return .None
}

doToLine(min) // prints 0
doToLine(mid) // prints 0.5
doToLine(max) // prints 1

How can I make a Swift enum with UIColor value?

I do it like this (basically using a struct as a namespace):

extension UIColor {
struct MyTheme {
static var firstColor: UIColor { return UIColor(red: 1, green: 0, blue: 0, alpha: 1) }
static var secondColor: UIColor { return UIColor(red: 0, green: 1, blue: 0, alpha: 1) }
}
}

And you use it like:

UIColor.MyTheme.firstColor

So you can have a red color inside your custom theme.

How to convert a computed value to a literal for enum initialization

You can only use literals for the raw values of type-backed enums.

To get this to work, you have to calculate the raw value of the calculation you're performing and paste that literal in as an approximation:

public enum ANGLE_TYPE : Double {
case DEGREES = 0.0174532925199433
case RADIANS = 1.0
}

The only other option is to not have a type-backed enum and manually provide the rawValue property:

public enum ANGLE_TYPE {
case DEGREES, RADIANS

var rawValue: Double {
get {
switch self {
case .DEGREES:
return Double(CGFloat(M_PI / 180.0))
case .RADIANS:
return 1.0
}
}
}
}

This might make sense because this means you don't have the init(rawValue:Double) initializer, which doesn't make a whole lot of sense in this case probably.

As a side note, this all caps thing is really unnecessary. I'd much prefer something more like this:

public enum AngleMeasureUnit {
case Degrees, Radians
}

Can I bind different associated values in a Swift enum to the same var?

That's an interesting case: An enum somehow isn't the right data type, because the value isn't either in radians or in degrees, both are just angles and not really different things. Also typealias Radians = Double wouldn't work because there's no unit safety.

Maybe you can use something like this though:

import Darwin

struct Angle {
enum Unit {
case Radians
case Degrees
case Rotations

var radiansFactor : Double {
switch self {
case Radians: return 1
case Degrees: return 180.0 / M_PI
case Rotations: return 1 / 2 / M_PI
}
}
}

var unit : Unit {
didSet {
value /= oldValue.radiansFactor
value *= unit.radiansFactor
}
}
var value : Double
}

func * (var lhs: Angle, rhs: Double) -> Angle {
lhs.value *= rhs
return lhs
}

var angle = Angle(unit: .Degrees, value: 180)

angle.value // 180.0
angle.unit = .Radians
angle.value // 3.141592...
angle.unit = .Rotations
angle.value // 0.5

Oh and as for the answer to your original question: No you cannot.

Can Swift enums have multiple raw values?

No, an enum cannot have multiple raw values - it has to be a single value, implementing the Equatable protocol, and be literal-convertible as described in the documentation.

I think the best approach in your case is to use the error code as raw value, and a property backed by a prepopulated static dictionary with the error code as key and the text as value.



Related Topics



Leave a reply



Submit