Post

Replies

Boosts

Views

Activity

Reply to Optional and onTapGesture
Here is the solution. Instead of using onTapGesture, I used a Observable class where I stored all the data from the tag. struct NFCWriteButton : UIViewRepresentable {     @Binding var data: String     @ObservedObject var dataToWrite: NFCData     func updateUIView(_ uiView: UIButton, context: Context) {    }     func makeUIView(context: UIViewRepresentableContext<NFCWriteButton>) -> UIButton {         let button = UIButton()         button.setTitle("Write on Tag", for: .normal)         button.addTarget(context.coordinator, action: #selector(context.coordinator.beginScan(_:)), for: .touchUpInside)         return button     }     func makeCoordinator() -> NFCWriteButton.Coordinator {         return Coordinator(data: $data, toWrite: _dataToWrite)     }     class Coordinator: NSObject, NFCNDEFReaderSessionDelegate {         var session : NFCNDEFReaderSession?         @ObservedObject var dataToWrite: NFCDataSec = NFCDataSec()         @Binding var data: String         @ObservedObject var nfcData: NFCData         @objc func beginScan(_ sender: Any) {             guard NFCNDEFReaderSession.readingAvailable else {                 print("error: Scanning not support")                 return             }             session = NFCNDEFReaderSession(delegate: self, queue: .main, invalidateAfterFirstRead: true)             session?.alertMessage = "Hold your iphone near to scan."             session?.begin()         }         init(data: Binding<String>, toWrite: ObservedObject<NFCData>) {             _data = data             _nfcData = toWrite         }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Networking error management
You usually use (your own) completion handler to tell error to the caller, but in your case, adding a new @Published property holding the error would be an alternative. So in my class I should be adding a @Published property variable to handle errors ? This is why I would like to implement a throw scenario to my request Button(action: {     self.showGroup = false     if (connexion.checkEmpty) { &#9;&#9;&#9;&#9;do {             try self.connect(url: "url")         } catch {             self.showError = true         } &#9;  } else {         self.showEmpty = true     } }) {     Text("button") } If iOS doesn't allow me to make throws is it better to make small functions for error management.             URLSession.shared.dataTask(with: request) { data, res, error in                 guard let httpResponse = res as? HTTPURLResponse,                         (200...299).contains(httpResponse.statusCode) else {                         self.handleServerError(res)                     return func handleServerError(_ res: URLResponse?) {     print("ERROR: Status Code: \(res!): the status code MUST be between 200 and 299") } Also, why can't I implement throws in my function when it is throwing ? func connect(url: String) throws { This is an exemple of the POST {"email":"XXXX@hell.com","password":"123456!"} Settings : KEY = Content-Type VALUE = application/json Here is and example of the dataString I'm receiving: [{"idTransfert":[],"_id":"60099ddc0eb9734ede9e831d","email":"test@test.eu","password":"4f8c9f9b94527112dbc","tokens":[{"_id":"60099de20eb9734ede9e8320","token":"eyJhbGciVCJ9.eyJfaWQ9.nEh2K7W0"},{"_id":"60099e160eb9734ede9e8323","token":"eyJhbGciOiJI.eyJfaWQiOiI2M9.nkJRk134b0"}],"sessions":[{"_id":"60099e160eb9734ede9e8325","sessionId":"nQIGGmN-766Z6nLwJBwSuLsQwlwP-WFu"}],"__v":2}] Here another version of my class: class Connexion: Codable, ObservableObject {     enum CodingKeys: String, CodingKey {         case email, password, id, token, sessionID     } /*   @Published var token: [Token] = [.init()] */ /*   @Published var sessionID: [SessionID] = [.init()]*/     @Published var token: String = ""     @Published var sessionID: String = ""     @Published var id: String = ""     @Published var email: String = ""     @Published var password: String = ""     init() {    }     required init(from decoder: Decoder) throws {         let container = try decoder.container(keyedBy: CodingKeys.self) /*        sessionID = try container.decode([SessionID].self, forKey: .sessionID)*/ /*        token = try container.decode([Token].self, forKey: .token)*/         sessionID = try container.decode(String.self, forKey: .sessionID)         token = try container.decode(String.self, forKey: .token)         id = try container.decode(String.self, forKey: .id)     }     func encode(to encoder: Encoder) throws {         var container = encoder.container(keyedBy: CodingKeys.self)         try container.encode(email, forKey: .email)         try container.encode(password, forKey: .password)     }     var checkEmpty: Bool {         if (email.isEmpty || password.isEmpty) {             return false         }         return true     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Networking error management
I have this error when printing from line 44 of the code you send. Invalid response typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil)) - I don't have other setting Http Method: POST Body: raw: {"email":"XXXX@hell.com","password":"123456!"} Headers :  KEY = Content-Type VALUE = application/json Here is and example of the dataString I'm receiving from my request in my code: [ { "idTransfert":[]," _id":"60099ddc0eb9734ede9e831d", "email":"test@test.eu", "password":"4f8c9f9b94527112dbc", "tokens":[ { "_id":"60099de20eb9734ede9e8320", "token":"eyJhbGciVCJ9.eyJfaWQ9.nEh2K7W0" }, { "_id":"60099e160eb9734ede9e8323", "token":"eyJhbGciOiJI.eyJfaWQiOiI2M9.nkJR k134b0" } ], "sessions": [ { "_id":"60099e160eb9734ede9e8325", "sessionId":"nQIGGmN-766Z6nLwJBwSuLsQwlwP-WFu" } ], "__v":2 } ] API response: status 200 { &#9;&#9;"id": "5fc565209ce8b43c1315da9b", &#9;&#9;"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9", &#9;&#9;"sessionID": "xiJOIhPj4l1qYz9uQRwAnWN38QBy12Qp" }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Networking error management
I edited y Connexion Class: class Connexion: Codable, ObservableObject {     enum CodingKeys: String, CodingKey {         case email, password, id = "_id", token = "tokens", sessionID = "sessions", idTransfert     }     @Published var token: [Token] = [.init()] /* 1 */     @Published var sessionID: [SessionID] = [.init()] /* 1 */     @Published var idTransfert: [String] = []     @Published var id: String = ""     @Published var email: String = ""     @Published var password: String = "" } (1) they both have _id and token or sessionId depending on the class. But I think what could cause the error is the array [ { ... } ] of the JSON. I tried to make make an array from the Codingkey like so: let container = try decoder.container(keyedBy: [CodingKeys].self) But Instance method 'container(keyedBy:)' requires that '[Connexion.CodingKeys]' conform to 'CodingKey' Do have any ideas ? PS: tbh their API sucks a lot
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21