1

I have this enum in swift

@objc(PaymentMethods)
public enum PaymentMethods: Int, RawRepresentable {
    public typealias RawValue = String
    case card
    case account
    case paypal

    public var rawValue: RawValue {
        switch self {
        case .card:
            return "CARD"
        case .account:
            return "ACCOUNT"
        case .paypal:
            return "PAYPAL"
        }
    }

    public init(rawValue: RawValue){
        switch rawValue {
        case "CARD":
            self = .card
        case "ACCOUNT":
            self = .account
        case "PAYPAL":
            self = .paypal
        default:
            self = .card
        }
    }
}

And this property in a class.

@objc public class SomeClass: ExtendingSomeOtherStuffs {
  var supportedPaymentMethods:[PaymentMethods]!
}

my problem is how to bridge supportedPaymentMethods into Objective-C and use it.

I have looked at this post and this but still can't figure it out. can someone help me out with an example at least.

Am trying to use this in Native-script and I need to expose that property from Swift to Objective

Roman Podymov
  • 4,168
  • 4
  • 30
  • 57
Mohammed
  • 33
  • 6
  • What Objective-C code do you have? – Roman Podymov Dec 09 '19 at 08:27
  • @RomanPodymov I don't have access to Objective-C code but thats generated behind the scenes of Nativescript. I just need to be sure that the property will be bridged to objective c – Mohammed Dec 09 '19 at 12:56
  • BTW, as an aside, the enumeration definition appears unusual. `PaymentMethods : Int` would normally mean the raw values are of type `Int`, yet they are re-defined, somewhat artificially, to be `String`. Also, you didn't have to specify `RawRepresentable`, it's implied, but it doesn't hurt. – Anatoli P Dec 10 '19 at 02:58
  • Have you placed the swift files inside the project Or compiled it as a framework? Did you try generating typings? – Manoj Dec 14 '19 at 21:19
  • @Manoj it has been compiled as a Framework. When I generate typings it does not include any property that is of type Array of Enums. Just like above – Mohammed Dec 15 '19 at 11:19

1 Answers1

0

You can do it like so:

@objc public class SomeClass: NSObject {
    var supportedPaymentMethods: [PaymentMethods]
    
    @objc init(supportedPaymentMethods: [String]) {
        self.supportedPaymentMethods = supportedPaymentMethods.map { .init(rawValue: $0) }
    }
}

and use it like this in your Objective-C code:

[[SomeClass alloc] initWithSupportedPaymentMethods:@[@"ACCOUNT", @"CARD", @"PAYPAL"]];

If you don't want your class initializer to take any String I would suggest doing the following:

  1. Make your init(rawValue:) failable and return nil when the argument is invalid:
public init?(rawValue: RawValue) {
        switch rawValue {
        case "CARD":
            self = .card
        case "ACCOUNT":
            self = .account
        case "PAYPAL":
            self = .paypal
        default:
            return nil
        }
    }
  1. Use compactMap instead of map in the init of the SomeClass like so:
@objc init(supportedPaymentMethods: [String]) {
        self.supportedPaymentMethods = supportedPaymentMethods.compactMap { .init(rawValue: $0) }
    }

(it will eliminate the nil values)

narek.sv
  • 845
  • 5
  • 22