Crash when setting enum from Swift to Obj-C via setValue

I have some very simple Obj-Code:

typedef NS_ENUM(NSUInteger, PersonType) {
  Single = 0,
  Married = 1
};

@interface Person : NSObject
@property (nonatomic, assign) PersonType personType;
@property (nonatomic, assign) NSInteger intPersonType;
@end

@implementation Person
@end

When trying to setValue on the personType property, the app crashes. setValue works for any other property type (ints, strings etc) but crashes the moment I try this with an enum. See the following code:

    let person = Person()
    // OK
    person.personType = .Married
    
    // OK
    let existingType = person.value(forKey: #keyPath(Person.personType))
    
    // OK
    person.setValue(2, forKey: #keyPath(Person.intPersonType))
	
	// OK
    person.setValue(PersonType.Married.rawValue, forKey: #keyPath(Person.personType))	
    
    // Crashes with: reason: '-[__SwiftValue longLongValue]: unrecognized selector sent to instance 0x600002e77f60'
    person.setValue(PersonType.Married, forKey: #keyPath(Person.personType))

I would have thought since the enum has been declared in Obj-C, it would perform the needed conversion to make sure it gets assigned correctly.

Terms of Service

Privacy Policy

Cookie Policy