@@ -145,7 +145,7 @@ class SwiftGenerator extends StructuredGenerator<SwiftOptions> {
145
145
indent.nest (1 , () {
146
146
if (customType.type == CustomTypes .customEnum) {
147
147
indent.writeln (
148
- 'let enumResultAsInt: Int? = nilOrValue(self.readValue() as? Int)' );
148
+ 'let enumResultAsInt: Int? = nilOrValue(self.readValue() as! Int? )' );
149
149
indent.writeScoped ('if let enumResultAsInt = enumResultAsInt {' , '}' ,
150
150
() {
151
151
indent.writeln (
@@ -419,7 +419,7 @@ if (wrapped == nil) {
419
419
}
420
420
421
421
void _writeClassField (Indent indent, NamedType field, {bool addNil = true }) {
422
- indent.add ('${field .name }: ${_nullsafeSwiftTypeForDartType (field .type )}' );
422
+ indent.add ('${field .name }: ${_nullSafeSwiftTypeForDartType (field .type )}' );
423
423
final String defaultNil = field.type.isNullable && addNil ? ' = nil' : '' ;
424
424
indent.add (defaultNil);
425
425
}
@@ -487,7 +487,17 @@ if (wrapped == nil) {
487
487
getFieldsInSerializationOrder (classDefinition).last == field
488
488
? ''
489
489
: ',' ;
490
- indent.writeln ('${field .name }: ${field .name }$comma ' );
490
+ // Force-casting nullable enums in maps doesn't work the same as other types.
491
+ // It needs soft-casting followed by force unwrapping.
492
+ final String forceUnwrapMapWithNullableEnums =
493
+ (field.type.baseName == 'Map' &&
494
+ ! field.type.isNullable &&
495
+ field.type.typeArguments
496
+ .any ((TypeDeclaration type) => type.isEnum))
497
+ ? '!'
498
+ : '' ;
499
+ indent.writeln (
500
+ '${field .name }: ${field .name }$forceUnwrapMapWithNullableEnums $comma ' );
491
501
}
492
502
});
493
503
});
@@ -650,14 +660,11 @@ if (wrapped == nil) {
650
660
assert (! type.isVoid);
651
661
if (type.baseName == 'Object' ) {
652
662
return value + (type.isNullable ? '' : '!' );
653
- } else if (type.baseName == 'int' ) {
654
- if (type.isNullable) {
655
- // Nullable ints need to check for NSNull, and Int32 before casting can be done safely.
656
- // This nested ternary is a necessary evil to avoid less efficient conversions.
657
- return 'isNullish($value ) ? nil : ($value is Int64? ? $value as! Int64? : Int64($value as! Int32))' ;
658
- } else {
659
- return '$value is Int64 ? $value as! Int64 : Int64($value as! Int32)' ;
660
- }
663
+ // Force-casting nullable enums in maps doesn't work the same as other types.
664
+ // It needs soft-casting followed by force unwrapping.
665
+ } else if (type.baseName == 'Map' &&
666
+ type.typeArguments.any ((TypeDeclaration type) => type.isEnum)) {
667
+ return '$value as? ${_swiftTypeForDartType (type )}' ;
661
668
} else if (type.isNullable) {
662
669
return 'nilOrValue($value )' ;
663
670
} else {
@@ -846,7 +853,13 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
846
853
fieldType: fieldType,
847
854
type: returnType,
848
855
);
849
- indent.writeln ('completion(.success(result))' );
856
+ // There is a swift bug with unwrapping maps of nullable Enums;
857
+ final String enumMapForceUnwrap = returnType.baseName == 'Map' &&
858
+ returnType.typeArguments
859
+ .any ((TypeDeclaration type) => type.isEnum)
860
+ ? '!'
861
+ : '' ;
862
+ indent.writeln ('completion(.success(result$enumMapForceUnwrap ))' );
850
863
}
851
864
});
852
865
});
@@ -887,6 +900,12 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
887
900
final String argName = _getSafeArgumentName (index, arg.namedType);
888
901
final String argIndex = 'args[$index ]' ;
889
902
final String fieldType = _swiftTypeForDartType (arg.type);
903
+ // There is a swift bug with unwrapping maps of nullable Enums;
904
+ final String enumMapForceUnwrap = arg.type.baseName == 'Map' &&
905
+ arg.type.typeArguments
906
+ .any ((TypeDeclaration type) => type.isEnum)
907
+ ? '!'
908
+ : '' ;
890
909
891
910
_writeGenericCasting (
892
911
indent: indent,
@@ -896,9 +915,10 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
896
915
type: arg.type);
897
916
898
917
if (arg.label == '_' ) {
899
- methodArgument.add (argName);
918
+ methodArgument.add ('$ argName $ enumMapForceUnwrap ' );
900
919
} else {
901
- methodArgument.add ('${arg .label ?? arg .name }: $argName ' );
920
+ methodArgument
921
+ .add ('${arg .label ?? arg .name }: $argName $enumMapForceUnwrap ' );
902
922
}
903
923
});
904
924
}
@@ -1022,9 +1042,9 @@ String _swiftTypeForBuiltinGenericDartType(TypeDeclaration type) {
1022
1042
}
1023
1043
} else {
1024
1044
if (type.baseName == 'List' ) {
1025
- return '[${_nullsafeSwiftTypeForDartType (type .typeArguments .first )}]' ;
1045
+ return '[${_nullSafeSwiftTypeForDartType (type .typeArguments .first )}]' ;
1026
1046
} else if (type.baseName == 'Map' ) {
1027
- return '[${_nullsafeSwiftTypeForDartType (type .typeArguments .first )}: ${_nullsafeSwiftTypeForDartType (type .typeArguments .last )}]' ;
1047
+ return '[${_nullSafeSwiftTypeForDartType (type .typeArguments .first )}: ${_nullSafeSwiftTypeForDartType (type .typeArguments .last )}]' ;
1028
1048
} else {
1029
1049
return '${type .baseName }<${_flattenTypeArguments (type .typeArguments )}>' ;
1030
1050
}
@@ -1058,7 +1078,7 @@ String _swiftTypeForDartType(TypeDeclaration type) {
1058
1078
return _swiftTypeForBuiltinDartType (type) ?? type.baseName;
1059
1079
}
1060
1080
1061
- String _nullsafeSwiftTypeForDartType (TypeDeclaration type) {
1081
+ String _nullSafeSwiftTypeForDartType (TypeDeclaration type) {
1062
1082
final String nullSafe = type.isNullable ? '?' : '' ;
1063
1083
return '${_swiftTypeForDartType (type )}$nullSafe ' ;
1064
1084
}
@@ -1080,10 +1100,10 @@ String _getMethodSignature({
1080
1100
swiftFunction: swiftFunction,
1081
1101
);
1082
1102
final String returnTypeString =
1083
- returnType.isVoid ? 'Void' : _nullsafeSwiftTypeForDartType (returnType);
1103
+ returnType.isVoid ? 'Void' : _nullSafeSwiftTypeForDartType (returnType);
1084
1104
1085
1105
final Iterable <String > types =
1086
- parameters.map ((NamedType e) => _nullsafeSwiftTypeForDartType (e.type));
1106
+ parameters.map ((NamedType e) => _nullSafeSwiftTypeForDartType (e.type));
1087
1107
final Iterable <String > labels = indexMap (components.arguments,
1088
1108
(int index, _SwiftFunctionArgument argument) {
1089
1109
return argument.label ?? _getArgumentName (index, argument.namedType);
0 commit comments