diff --git a/pkgs/swift2objc/lib/src/transformer/_core/utils.dart b/pkgs/swift2objc/lib/src/transformer/_core/utils.dart index 429285e66..e594a24ac 100644 --- a/pkgs/swift2objc/lib/src/transformer/_core/utils.dart +++ b/pkgs/swift2objc/lib/src/transformer/_core/utils.dart @@ -66,7 +66,12 @@ import 'unique_namer.dart'; return (value, type); } } else if (type is OptionalType) { - final (newValue, newType) = maybeUnwrapValue(type.child, '$value?'); + final optValue = '$value?'; + var (newValue, newType) = maybeUnwrapValue(type.child, optValue); + if (newValue == optValue) { + // newValue is value?, so the ? isn't necessary and causes compile errors. + newValue = value; + } return (newValue, OptionalType(newType)); } else { throw UnimplementedError('Unknown type: $type'); diff --git a/pkgs/swift2objc/test/integration/optional_input.swift b/pkgs/swift2objc/test/integration/optional_input.swift index 5552fbf90..af19d6dd7 100644 --- a/pkgs/swift2objc/test/integration/optional_input.swift +++ b/pkgs/swift2objc/test/integration/optional_input.swift @@ -1,11 +1,11 @@ import Foundation -// public func funcOptionalPrimitiveReturn() -> Int? { return 123 } public func funcOptionalClassReturn() -> MyClass? { return MyClass(label: nil) } public func funcOptionalStructReturn() -> MyStruct? { return nil } public func funcOptionalArgs(label param: MyClass?) -> MyClass { return param! } public func funcMultipleOptionalArgs( label1 param1: MyClass?,label2 param2: Int, label3 param3: MyStruct?){} +public func funcOptionalStrings(str: String?) -> String? { return str; } public var globalOptional: MyStruct? diff --git a/pkgs/swift2objc/test/integration/optional_output.swift b/pkgs/swift2objc/test/integration/optional_output.swift index 920c486e6..add55f0c4 100644 --- a/pkgs/swift2objc/test/integration/optional_output.swift +++ b/pkgs/swift2objc/test/integration/optional_output.swift @@ -17,6 +17,10 @@ import Foundation return MyClassWrapper(result) } + @objc static public func funcOptionalStringsWrapper(str: String?) -> String? { + return funcOptionalStrings(str: str) + } + @objc static public func funcOptionalClassReturnWrapper() -> MyClassWrapper? { let result = funcOptionalClassReturn() return result == nil ? nil : MyClassWrapper(result!)