混编项目Release构建报错:rawValue重复声明问题求助
Hey there, I’ve run into this exact issue before—let’s break down what’s happening and how to fix it.
Why This Error Happens
When you define an enum using NS_ENUM in Objective-C, Swift automatically generates a RawRepresentable conformance for it with UInt as the raw value type (since your enum is based on NSUInteger).
Your manual extension tries to redefine RawRepresentable with String as the raw value, which creates a direct conflict: the auto-generated rawValue (for UInt) and your custom rawValue (for String) are both trying to exist on the same type. Debug builds might let this slide for flexibility, but Release builds enable stricter type checking and optimization, so it throws the "Invalid redeclaration" error.
Solution 1: Add Custom String Properties (No OC Changes)
If you don’t want to modify your Objective-C code, skip reimplementing RawRepresentable entirely. Instead, add custom computed properties and initializers for string conversion:
extension ABCCategory { // Custom string value instead of overriding rawValue var stringValue: String { switch self { case .first: return "first" case .second: return "second" @unknown default: return "first" } } // Custom initializer from string init?(stringValue: String) { switch stringValue { case "first": self = .first case "second": self = .second default: return nil } } }
You can now use myCategory.stringValue to get the string representation, and ABCCategory(stringValue: "first") to initialize from a string—no conflict with the auto-generated rawValue.
Solution 2: Switch to NS_STRING_ENUM in OC (Cleaner for String Raw Values)
If you want the enum to natively use String as its raw value in Swift, modify your Objective-C definition to use NS_STRING_ENUM instead of NS_ENUM:
In your .h file:
typedef NSString *ABCCategory NS_STRING_ENUM; extern ABCCategory const ABCCategoryFirst; extern ABCCategory const ABCCategorySecond;
In your .m file:
ABCCategory const ABCCategoryFirst = @"first"; ABCCategory const ABCCategorySecond = @"second";
Now Swift will automatically recognize this as a string-based enum. You can use ABCCategory.first.rawValue directly to get "first", and initialize with ABCCategory(rawValue: "first")—no manual extensions needed at all.
Why Release Mode Only?
Debug builds prioritize compilation speed and flexibility, so they often overlook redundant declarations. Release builds, however, enable full optimization and strict type checking to catch conflicts that could cause runtime issues or bloated code.
内容的提问来源于stack exchange,提问作者bitemybyte

