I try to build a string from an enum in Objective-C:
+ (NSString *)getStringFromKey:(ITGenericNotification)key {
return [NSString stringWithFormat:@"notification%lu", (long)key];
}
This code worked really well for over a year
However when I try to call it from swift with:
let notificationKey = NSNotification.getStringFromKey(ITNotificationWithObject.DidChangedQuestion.hashValue)
I get a corrupted string :
What is happening ? The value of ITGenericNotification seems to be correct,
ITGenericNotification
is defined as typedef long ITGenericNotification;
and is used as base type for an enum with typedef NS_ENUM(ITGenericNotification, ITNotificationWithObject)
And + (NSString *)getStringFromKey:(ITGenericNotification)key
is implemented as a category to NSNotification
You probably want .rawValue
, not .hashValue
, to get the underlying
integer value of the enum.
(Also the debugger variables view is sometimes wrong. When in doubt,
add a println()
or NSLog()
to your code to verify the values of
your variables.)