Skip to content

Commit f06278b

Browse files
authored
Fix optional wrapping issue in mapToStrings function by explicitly… (#24)
Fix optional wrapping issue in mapToStrings function by explicitedly allowing Any? values then unwrapping them.
1 parent fd06a43 commit f06278b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Sources/SegmentFirebase/FirebaseDestination.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,19 @@ extension FirebaseDestination {
201201
}
202202

203203
// Makes sure all traits are string based for Firebase API
204-
func mapToStrings(_ mapDictionary: [String: Any], finalize: (String, String) -> Void) {
204+
func mapToStrings(_ mapDictionary: [String: Any?], finalize: (String, String) -> Void) {
205205

206206
for (key, data) in mapDictionary {
207-
var dataString = data as? String ?? "\(data)"
208-
let keyString = key.replacingOccurrences(of: " ", with: "_")
209-
dataString = dataString.trimmingCharacters(in: .whitespacesAndNewlines)
210-
finalize(keyString, dataString)
207+
208+
// Since dictionary values can be Optional we have to unwrap them
209+
// before encoding so that we don't encode them as "Optional(*)"
210+
// Note: nil values are NOT encoded.
211+
if let d = data {
212+
var dataString = d as? String ?? "\(d)"
213+
let keyString = key.replacingOccurrences(of: " ", with: "_")
214+
dataString = dataString.trimmingCharacters(in: .whitespacesAndNewlines)
215+
finalize(keyString, dataString)
216+
}
211217
}
212218
}
213219
}

0 commit comments

Comments
 (0)