[ad_1]
i’ve this struct import Basis
import Basis
// MARK: - Welcome
struct Say: Codable {
let response: [Rest]
init(response: [Rest]) {
self.response = response
}
}
// MARK: - Response
struct Relaxation: Codable {
let eventID: String
let information: ClassMe
enum CodingKeys: String, CodingKey {
case eventID = "event_id"
case information
}
init(eventID: String, information: ClassMe) {
self.eventID = eventID
self.information = information
}
}
// MARK: - DataClass
struct ClassMe: Codable {
let toTime, to, addInfo, maxWeight: String
let freeSpace, kind, fromTime: String
let blocked: Blocked
let fromDate: String
let index: Int
let toDate, price, from: String
let isDeleted: Bool
let userID: String
enum CodingKeys: String, CodingKey {
case toTime, to, addInfo, maxWeight, freeSpace, kind, blocked, fromTime, fromDate, index
case toDate = "ToDate"
case price, from
case isDeleted = "is_deleted"
case userID = "userId"
}
init(toTime: String, to: String, addInfo: String, maxWeight: String, freeSpace: String, kind: String, blocked: Blocked, fromTime: String, fromDate: String, index: Int, toDate: String, price: String, from: String, isDeleted: Bool, userID: String) {
self.toTime = toTime
self.to= to
self.addInfo = addInfo
self.maxWeight = maxWeight
self.freeSpace = freeSpace
self.kind = kind
self.blocked = blocked
self.fromTime = fromTime
self.fromDate = fromDate
self.index = index
self.toDate = toDate
self.price = price
self.from = from
self.isDeleted = isDeleted
self.userID = userID
}
}
enum Blocked: Codable {
case anythingArray([JSONAny])
case string(String)
init(from decoder: Decoder) throws {
let container = attempt decoder.singleValueContainer()
if let x = attempt? container.decode([JSONAny].self) {
self = .anythingArray(x)
return
}
if let x = attempt? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(Blocked.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Unsuitable kind for Blocked"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
swap self {
case .anythingArray(let x):
attempt container.encode(x)
case .string(let x):
attempt container.encode(x)
}
}
}
and if i need to save in core inside an http request i can’t for i
func httpfetchEventFly(){
// declare the parameter as a dictionary that comprises string as key and worth mixture. contemplating inputs are legitimate
let dateFormatter = DateFormatter()
let t: [Int] = Array(0...100)
// var i: Int = 0
@State var i = 0
let parameters:Dictionary<String, Any> = [
"data" : [
"date":"07-03-2022",
"type":"airplane"
]
]
// create the url with URL
let url = URL(string: "myURL")! // change server url accordingly
// create the session object
let session = URLSession.shared
// now create the URLRequest object utilizing the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http technique as POST
request.httpBody = parameters.percentEncoded()
// add headers for the request
request.addValue("utility/json; charset=utf-8", forHTTPHeaderField: "Content material-Sort") // change as per server necessities
request.addValue("utility/json; charset=utf-8", forHTTPHeaderField: "Settle for")
do {
// convert parameters to Information and assign dictionary to httpBody of request
request.httpBody = attempt JSONSerialization.information(withJSONObject: parameters, choices: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
return
}
// create dataTask utilizing the session object to ship information to the server
let job = session.dataTask(with: request) { information, response, error in
if let error = error {
print("Put up Request Error: (error.localizedDescription)")
return
}
// guarantee there may be legitimate response code returned from this HTTP response
guard let httpResponse = response as? HTTPURLResponse,
(200...299).comprises(httpResponse.statusCode)
else {
print("Invalid Response obtained from the server")
return
}
// guarantee there may be information returned
guard let responseData = information else {
print("nil Information obtained from the server")
return
}
do {
// create json object from information or use JSONDecoder to transform to Mannequin stuct
if let jsonResponse = attempt JSONSerialization.jsonObject(with: responseData, choices: .mutableContainers) as? Dictionary<String,Any>{
print(jsonResponse)
let decoder = JSONDecoder()
do {
let loginResponse = attempt decoder.decode(Say.self, from: responseData)
for i in t.prefix(120) { ← ((((that doesn’t work i need a resolution for the index i))))
print("fromdate:", loginResponse.response[i].information.fromDate)
let peerfly = Peerfly(context: self.persistentContainer.viewContext)
//peertraffic.fromDate = dateFormatter.date(from: loginResponse.response[i].information.fromDate)
peerfly.fromDate = loginResponse.response[i].information.fromDate
peerfly.toDate = loginResponse.response[i].information.toDate
peerfly.freeSpace = loginResponse.response[i].information.freeSpace
peerfly.from = loginResponse.response[i].information.from
peerfly.to = loginResponse.response[i].information.to
peerfly.fromTime = loginResponse.response[i].information.fromTime
peerfly.toTime = loginResponse.response[i].information.fromTime
peerfly.price = loginResponse.response[i].information.price
peerfly.maxWeight = loginResponse.response[i].information.maxWeight
peerfly.addInfo = loginResponse.response[i].information.addInfo
peerfly.eventID = loginResponse.response[i].eventID
}
do {
attempt self.persistentContainer.viewContext.save()
} catch {
print("Failed to save lots of occasion (error)")
}
DispatchQueue.most important.async {
self.occasions = loginResponse
}
} catch {
print(error)
}
// deal with json response
} else {
print("information perhaps corrupted or in mistaken format")
throw URLError(.badServerResponse)
}
} catch let error {
print(error.localizedDescription)
}
}
// carry out the duty
job.resume()
}
}
}
i need an index to loop however when it reaches a null worth it doesn’t do deadly error ,and out of vary , i simply need to take the index that has information which is the response array and if is null it does nothing ( neither prints or throw deadly error ), however cannot do it
figuring out that i used persistent as a result of i’m storing in core information
thanks very a lot my associates
[ad_2]
