-1

I am getting a crash when I cast my singleResult (of Type Result<FAQResult>) to a FAQResult

fileprivate var fAQS = [FAQ]()

 when(resolved: task).then { (result) -> Void in
       let singleResult:Result<FAQResult> = result.first!     
       let whereItCrashes = singleResult as! FAQResult
       self.fAQS =  whereItCrashes.result!
    }

Cast from Result<FAQResult> to unrelated type FAQResult always fails

The code under is from a core-promise pod used in the project. All it does it give me back a .boolValue, but I need to convert it from a Results to a FAQResult.

    public enum Result<T> {
    /// Fulfillment
    case fulfilled(T)
    /// Rejection
    case rejected(Error)

    init(_ resolution: Resolution<T>) {
        switch resolution {
        case .fulfilled(let value):
            self = .fulfilled(value)
        case .rejected(let error, _):
            self = .rejected(error)
        }
    }

    /**
     - Returns: `true` if the result is `fulfilled` or `false` if it is `rejected`.
     */
    public var boolValue: Bool {
        switch self {
        case .fulfilled:
            return true
        case .rejected:
            return false
        }
    }
}

Any solutions on how to let it cast correctly?

iCappsCVA
  • 47
  • 6
  • 3
    The error message is quite clear, you cant cast Result to FAQResult, they are different thing. https://stackoverflow.com/questions/24263539/accessing-an-enumeration-association-value-in-swift this post tells you how to get the associated value from enum – Surely Jan 30 '18 at 15:08

1 Answers1

0

You may add new computed property value to Result<T> like this:

/**
 - Returns: actual fulfilled `value` if the result is `fulfilled` or `nil` if it is `rejected`.
*/
public var value: T? {
    switch self {
    case .fulfilled(let value):
        return value
    case .rejected:
        return nil
    }
}

And then singleResult.value will return you FAQResult.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
user28434'mstep
  • 6,290
  • 2
  • 20
  • 35