Skip to content

Commit

Permalink
Fix bug with optional string params (#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe authored Dec 1, 2024
1 parent 25f36d6 commit f59efc7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pkgs/swift2objc/lib/src/transformer/_core/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion pkgs/swift2objc/test/integration/optional_input.swift
Original file line number Diff line number Diff line change
@@ -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?

Expand Down
4 changes: 4 additions & 0 deletions pkgs/swift2objc/test/integration/optional_output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down

0 comments on commit f59efc7

Please sign in to comment.