From a3310cd62e4489d25d4485040b3d980ded5c104a Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Mon, 18 Mar 2024 22:38:49 +0400 Subject: [PATCH] ios: fix decoder crashing (#3921) * ios: fix ios decoder crashing * change * fix * refactor * simplify --------- Co-authored-by: Evgeny Poberezkin --- apps/ios/SimpleXChat/API.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/ios/SimpleXChat/API.swift b/apps/ios/SimpleXChat/API.swift index 3c9f77d791..cdd1008c13 100644 --- a/apps/ios/SimpleXChat/API.swift +++ b/apps/ios/SimpleXChat/API.swift @@ -213,13 +213,11 @@ public func chatResponse(_ s: String) -> ChatResponse { } } else if type == "chatCmdError" { if let jError = jResp["chatCmdError"] as? NSDictionary { - let user: UserRef? = try? decodeObject(jError["user_"] as Any) - return .chatCmdError(user_: user, chatError: .invalidJSON(json: prettyJSON(jError) ?? "")) + return .chatCmdError(user_: decodeUser_(jError), chatError: .invalidJSON(json: prettyJSON(jError) ?? "")) } } else if type == "chatError" { if let jError = jResp["chatError"] as? NSDictionary { - let user: UserRef? = try? decodeObject(jError["user_"] as Any) - return .chatError(user_: user, chatError: .invalidJSON(json: prettyJSON(jError) ?? "")) + return .chatError(user_: decodeUser_(jError), chatError: .invalidJSON(json: prettyJSON(jError) ?? "")) } } } @@ -228,6 +226,14 @@ public func chatResponse(_ s: String) -> ChatResponse { return ChatResponse.response(type: type ?? "invalid", json: json ?? s) } +private func decodeUser_(_ jDict: NSDictionary) -> UserRef? { + if let user_ = jDict["user_"] { + try? decodeObject(user_ as Any) + } else { + nil + } +} + func parseChatData(_ jChat: Any) throws -> ChatData { let jChatDict = jChat as! NSDictionary let chatInfo: ChatInfo = try decodeObject(jChatDict["chatInfo"]!)