nicklockwood / FXForms

[DEPRECATED]
Other
2.93k stars 339 forks source link

Cannot select field if type is a Swift enumeration #438

Closed realenginerd closed 8 years ago

realenginerd commented 8 years ago

I intend to use a Swift enumeration as a field. This is my current code:

enum Gender
{
    case male
    case female
    case undetermined
}

var gender: Gender = Gender.undetermined

func genderField() -> [String: AnyObject]
{
    return [
        FXFormFieldDefaultValue: "Undetermined",
        FXFormFieldOptions: ["Male", "Female", "Undetermined"]
    ]
}

However, this results in a field that is not selectable. It only becomes selectable when I change the type to Int:

var gender: Int = 2

What am I missing? It looks like the sample RegistrationForm.swift does not do what I am doing, so that makes me question if what I am doing here is correct or even supported. Can someone clarify? Thanks in advance.

nicklockwood commented 8 years ago

Swift enums are very different from C enums, and I've not tested FXForms with them, so I can't be sure they can be used in the same way as in my example code.

With that said, did you try:

enum Gender : Int {
  case male
  case female
  case undetermined
}

Perhaps that will work?

realenginerd commented 8 years ago

Weird -- doing that makes the field disappear entirely. I've tried both setting a default value for the field (var gender: Gender = Gender.undetermined), as well as leaving it an optional(var gender: Gender?), to no avail.

I could convert this field into a regular int for now, but I would prefer a native Swift way of working with this. Alternatively, I could convert this file to ObjC, using NS_ENUM instead of Swift enums. What is your recommendation?

realenginerd commented 8 years ago

Great news! I got it to work by moving the enum out of Swift and into Objective-C. I created a new .h file that contains the NS_ENUM:

typedef NS_ENUM(NSUInteger, Gender)
{
    Gendermale,
    Genderfemale,
    Genderundetermined
}

I added this header file to the bridging header so that my Swift form could access it. Happily, the field is no longer unselectable.

This solves my issue for now.