Post

Replies

Boosts

Views

Activity

How to accept a self-signed SSL certificate with URLSession and Combine
I'm using URLSession and Combine to monitor and control a device on my internal network. One URL to retrieve JSON data is https://ip_address/api/v1/items The device has a self-signed certificate so I'm getting an error that the certificate is invalid. I need my code to ignore the certificate and retrieve the data. Is there a good solution to accept this self-signed certificate with Combine and URLSession? Here is the code that makes the HTTPS get request.     func sendControllerGetRequest(uri: String) -> PassthroughSubject<Data, Never> {         getRequestPublisher = PassthroughSubject<Data, Never>()         var urlRequest = URLRequest(url: URL(string: self.baseURI+uri)!)         urlRequest.httpMethod = "GET"         urlRequest.setValue("Bearer \(self.directorBearerToken)", forHTTPHeaderField: "Authorization")         getRequestCancellable = URLSession.shared.dataTaskPublisher(for: urlRequest)             .map{ $0.data }             .sink(receiveCompletion: { completion in                 switch completion {                 case .failure(let error):                     print("sendControllerGetRequest error")                     print(error)                 case .finished:                     print("sendControllerGetRequest finished")                     break                 }             }, receiveValue: { requestDetails in                print(String(data: requestDetails, encoding: .utf8)!)                 print("sendControllerGetRequest()")                 self.getRequestPublisher.send(requestDetails)                 self.getRequestPublisher.send(completion: .finished)             }             )         return getRequestPublisher     }
1
0
7.1k
Nov ’20
SwiftUI Nested ForEach causes Fatal error: each layout item may only occur once
I'm trying to create a LazyVGrid view to display the contents of objects in an array using nested ForEach statements. Each object in the array contains an array. The code is causing an app crash with the message "Fatal error: each layout item may only occur once". Each object contains an array of values that need to be displayed as a row in the grid. The row consists of cell with the object's sku string followed by a number of cells with the integers from the object's array. I chose to use a class instead of a struct because I need to update the array within the class. This is the class for the objects in the array. class RoomPickupData: Identifiable { &#9;&#9;let id = UUID() &#9;&#9;var sku: String = "" &#9;&#9;// Array of counts for sku on a particular date &#9;&#9;var roomsForDate: [Date: Int] = [:] } In the code the first ForEach is used to put header information in the first line of the grid. The next ForEach and the ForEach nested in it are causing the error. The outer ForEach iterates through the array of objects so I can create a row in the grid for each object. Each row consists of a string followed values from the roomsForDate array. The size of the roomsForDate array is not fixed. If I comment out the nested ForEach the code executes but with it I get the fatal error. If I comment out Text(roomData.value.description) so there is noting in the inner ForEach, the code runs fine too. If I replace Text(roomData.value.description) with Text("") the app crashes. ForEach nesting seems like the best way to accomplish producing a grid view from a two dimensional array or array of objects each containing an array. I've found other posts showing that nested ForEach statements can be used in SwiftUI. Here is the code. Your help with this issue is appreciated. struct RoomTableView: View { &#9;&#9;@ObservedObject var checkfrontVM: CheckFrontVM &#9;&#9;let dateFormatter = DateFormatter() &#9;&#9; &#9;&#9;init(checkfrontVM: CheckFrontVM) { &#9;&#9;&#9;&#9;self.checkfrontVM = checkfrontVM &#9;&#9;&#9;&#9;dateFormatter.dateFormat = "MM/dd/yyyy" &#9;&#9;} &#9;&#9; &#9;&#9;func initColumns() -> [GridItem] { &#9;&#9;&#9;&#9;var columns: [GridItem] &#9;&#9;&#9;&#9;var columnCount = 0 &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;if self.checkfrontVM.roomPickupDataArray.first != nil { &#9;&#9;&#9;&#9;&#9;&#9;columnCount = self.checkfrontVM.roomPickupDataArray.first!.roomsForDate.count &#9;&#9;&#9;&#9;} else { &#9;&#9;&#9;&#9;&#9;&#9;return [] &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;columns = [GridItem(.flexible(minimum: 100))] + Array(repeating: .init(.flexible(minimum: 100)), count: columnCount) &#9;&#9;&#9;&#9;return columns &#9;&#9;} &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;if self.checkfrontVM.roomPickupDataArray.first != nil { &#9;&#9;&#9;&#9;&#9;&#9;ScrollView([.horizontal, .vertical]) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;LazyVGrid(columns: initColumns()) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Blank") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(self.checkfrontVM.roomPickupDataArray.first!.roomsForDate.sorted(by: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;$0 < $1 &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}), id: \.key) { roomData in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(dateFormatter.string(from: roomData.key)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(self.checkfrontVM.roomPickupDataArray) { roomPickupData in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Group { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(roomPickupData.sku) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(width: 80, alignment: .leading) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(roomTypeColor[roomPickupData.sku]) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(roomPickupData.roomsForDate.sorted(by: { $0.key < $1.key}), id: \.key) { roomData in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(roomData.value.description) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.frame(height: 600, alignment: .topLeading) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.padding(.all, 10) &#9;&#9;&#9;&#9;} &#9;&#9;} }
2
0
3.5k
Aug ’20