From ad6bd802679a5a1bb27a83207ff18fec5600d2ca Mon Sep 17 00:00:00 2001 From: Deling Ren Date: Tue, 10 Oct 2023 11:53:23 -0700 Subject: [PATCH] Fix the crash when an endpoint without a UTF8 name is discovered --- .../Example/iOS Example/Model/Model.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift b/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift index 6128816c..df9bd086 100644 --- a/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift +++ b/connections/swift/NearbyConnections/Example/iOS Example/Model/Model.swift @@ -120,10 +120,13 @@ class Model: ObservableObject { extension Model: DiscovererDelegate { func discoverer(_ discoverer: Discoverer, didFind endpointID: EndpointID, with context: Data) { + guard let endpointName = String(data: context, encoding: .utf8) else { + return + } let endpoint = DiscoveredEndpoint( id: UUID(), endpointID: endpointID, - endpointName: String(data: context, encoding: .utf8)! + endpointName: endpointName ) endpoints.insert(endpoint, at: 0) } @@ -138,10 +141,13 @@ extension Model: DiscovererDelegate { extension Model: AdvertiserDelegate { func advertiser(_ advertiser: Advertiser, didReceiveConnectionRequestFrom endpointID: EndpointID, with context: Data, connectionRequestHandler: @escaping (Bool) -> Void) { + guard let endpointName = String(data: context, encoding: .utf8) else { + return + } let endpoint = DiscoveredEndpoint( id: UUID(), endpointID: endpointID, - endpointName: String(data: context, encoding: .utf8)! + endpointName: endpointName ) endpoints.insert(endpoint, at: 0) connectionRequestHandler(true)