Skip to content

Commit e47e617

Browse files
committed
use switch instead of if/else if
1 parent 103fe80 commit e47e617

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

Sources/FlutterSwift/Codecs/Standard/FlutterStandardCodable.swift

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,34 @@ public protocol FlutterStandardCodable {
1414
/// extension for initializing a type from a type-erased value
1515
public extension AnyFlutterStandardCodable {
1616
init(_ any: Any) throws {
17-
if isNil(any) {
17+
guard !isNil(any) else {
1818
self = .nil
19-
} else if let bool = any as? Bool {
19+
return
20+
}
21+
switch any {
22+
case let bool as Bool:
2023
self = bool ? .true : .false
21-
} else if let int32 = any as? Int32 {
24+
case let int32 as Int32:
2225
self = .int32(int32)
23-
} else if let int64 = any as? Int64 {
26+
case let int64 as Int64:
2427
self = .int64(int64)
25-
} else if let float64 = any as? Double {
28+
case let float64 as Double:
2629
self = .float64(float64)
27-
} else if let string = any as? String {
30+
case let string as String:
2831
self = .string(string)
29-
} else if let uint8Data = any as? [UInt8] {
32+
case let uint8Data as [UInt8]:
3033
self = .uint8Data(uint8Data)
31-
} else if let int32Data = any as? [Int32] {
34+
case let int32Data as [Int32]:
3235
self = .int32Data(int32Data)
33-
} else if let int64Data = any as? [Int64] {
36+
case let int64Data as [Int64]:
3437
self = .int64Data(int64Data)
35-
} else if let float32Data = any as? [Float] {
38+
case let float32Data as [Float]:
3639
self = .float32Data(float32Data)
37-
} else if let float64Data = any as? [Double] {
40+
case let float64Data as [Double]:
3841
self = .float64Data(float64Data)
39-
} else if let list = any as? [Any] {
42+
case let list as [Any]:
4043
self = try .list(list.map { try AnyFlutterStandardCodable($0) })
41-
} else if let map = any as? [AnyHashable: Any] {
44+
case let map as [AnyHashable: Any]:
4245
self = try .map(map.reduce([:]) {
4346
var result = $0
4447
try result[AnyFlutterStandardCodable($1.key)] = AnyFlutterStandardCodable(
@@ -47,13 +50,13 @@ public extension AnyFlutterStandardCodable {
4750
)
4851
return result
4952
})
50-
} else if let any = any as? FlutterStandardCodable {
53+
case let any as FlutterStandardCodable:
5154
self = try any.bridgeToAnyFlutterStandardCodable()
52-
} else if let raw = any as? (any RawRepresentable) {
55+
case let raw as any RawRepresentable:
5356
self = try raw.bridgeToAnyFlutterStandardCodable()
54-
} else if let encodable = any as? Encodable {
57+
case let encodable as Encodable:
5558
self = try encodable.bridgeToAnyFlutterStandardCodable()
56-
} else {
59+
default:
5760
throw FlutterSwiftError.notRepresentableAsStandardField
5861
}
5962
}

0 commit comments

Comments
 (0)