how to remove double quote from empty array in codable struct - swift

I'm making a POST request with URLSession. I need to send the following string in the request body:
{"rebajados": false, "text": "pantalon", "municipios": [], "departamentos": []}
so i define a struct to use codable to send data as request body. the struct is this.
struct filter: Codable {
var text: String?
var departamentos: [String]?
var municipios: [String]?
var rebajados = false
}
but what I send is this:
{
"departamentos": [
""
],
"municipios": [
""
],
"rebajados": false,
"text": "pantalon"
}
The backend returns no result because [""] makes it lost.
So what's a posible way to make the array a empty array without the double ""?
Note: I can't modifiy the backend to accept the array with empty string.

First of all please name structs with a starting capital letter.
An empty string array is encoded as empty JSON array
struct Filter : Codable {
let text : String
let departamentos : [String]
let municipios : [String]
let rebajados : Bool
}
let filter = Filter(text: "pantalon", departamentos: [], municipios: [], rebajados: false)
do {
let data = try JSONEncoder().encode(filter)
let string = String(data: data, encoding: .utf8)!
print(string) // {"rebajados":false,"municipios":[],"departamentos":[],"text":"pantalon"}
} catch {
print(error)
}

Try
struct filter:Codable {
var text: String?
var departamentos: [String] = []
var municipios: [String] = []
var rebajados = false
}

Related

How to create a JSON in SWFT using users input data?

I need to create a JSON like this:
{
"var1": "1",
"var2": "2",
"newArray": [{
"Deleted": false
}]
}
My Codable looks like this:
struct RequestModel: Codable {
var var1: String?
var var1: String?
var Treatments: [Treat]
}
struct Treat: Codable {
var adminBy: String?
}
So, now I need to create the above JSON... I tried the following:
let adminBy = adminByTextField.text ?? ""
let VAR1 = var1TextField.text ?? ""
let VAR2 = var2TextField.text ?? ""
let dict = ["adminBy":adminBy] as! NSDictionary
///And I TRY to create my JSON here
private func createRequestObject() -> RequestModel {
let request = RequestModel(var1: VAR1,
var2: VAR2,
Treatments: dict)
return request
}
But I get this error:
I don't understand why this error occurs or how to fix it.
any advice would be appreciated.
You should not be using an NSDictionary here. You RequestModel object requires an array of Treat.
You could create the array in the following way and pass it to your RequestModel :
let adminBy = adminByTextField.text ?? ""
let VAR1 = var1TextField.text ?? ""
let VAR2 = var2TextField.text ?? ""
let treats = [Treat(adminBy: adminBy)]
let request = RequestModel(var1: VAR1, var2: VAR2, Treatments: treats)

Issues with Codable Arrays and Alamofire

I am attempting to submit the following request through Alamofire and I am receiving the following error:
2020-01-13 09:41:05.912103-0600 AFNetworkingDemo[29720:604258] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'
My assumption is that it is the way I am defining the arrays within the object (I was following some of the material found here: https://benscheirman.com/2017/06/swift-json)
struct ProgramRequest: Codable {
var userID: Int
var programData: ProgramData
var json: Constants.Json {
return [
"userID": userID,
"programData": programData.json,
]
}
}
struct ProgramData: Codable {
var airhotel: [AirHotel]
var security: [Security]
var json: Constants.Json {
return [
"airhotel": airhotel,
"security": security
]
}
}
struct AirHotel: Codable {
var id: Int
var loyaltyNumber: String
var json: Constants.Json {
return [
"id": id,
"loyaltyNumber": loyaltyNumber
]
}
}
struct Security: Codable {
var vendorCode: String
var loyaltyNumber: String
var json: [String: Any] {
return [
"vendorCode": vendorCode,
"loyaltyNumber": loyaltyNumber
]
}
}
The json dictionary at each level is to render the objects appropriately for Alamofire. For a given example, if I print it out using:
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try jsonEncoder.encode(programRequest)
if let jsonString = String(data: jsonData, encoding: .utf8) {
print(jsonString) #1
}
print("IsValidJSON: ", JSONSerialization.isValidJSONObject(programRequest)) #2
print(programRequest.json) #3
urlRequest = try! JSONEncoding.default.encode(urlRequest, with: programRequest.json) #4 causes error
1 output:
{
"userID" : 10021,
"programData" : {
"airhotel" : [],
"security" : [
{
"vendorCode" : "sty",
"loyaltyNumber" : "Loyal1"
}
]
}
}
2 output
IsValidJSON: false
3 output - I noticed the AFNetworkingDemo.Security within the output, is that what Alamofire JSONEncoding on:
["programData": ["security": [AFNetworkingDemo.Security(vendorCode: "sty", loyaltyNumber: "Loyal1")], "airhotel": []], "userID": 10021]
My question would be, what changes to I need to make in the AirHotel and Security sections of ProgramRequest in order to resolve my issues with Alamofire?
Step 1: Stop reinventing Codable
The json computed property is not needed and wrongly used.
Remove it.
struct ProgramRequest: Codable {
var userID: Int
var programData: ProgramData
}
struct ProgramData: Codable {
var airhotel: [AirHotel]
var security: [Security]
}
struct AirHotel: Codable {
var id: Int
var loyaltyNumber: String
}
struct Security: Codable {
var vendorCode: String
var loyaltyNumber: String
}
Step 2: Start using Codable
Now given a programRequest, eg. like this
let programRequest = ProgramRequest(userID: 1, programData: ProgramData(airhotel: [AirHotel(id: 1, loyaltyNumber: "loyaltyNumber")], security: [Security(vendorCode: "loyaltyNumber", loyaltyNumber: "loyaltyNumber")]))
You can generate the correct Data value representing the JSON simply writing
let data = try JSONEncoder().encode(programRequest)
And (if you want) you can convert data intro a String
let json = String(data: data, encoding: .utf8)
Output
Optional("{\"userID\":1,\"programData\":{\"airhotel\":[{\"id\":1,\"loyaltyNumber\":\"loyaltyNumber\"}],\"security\":[{\"vendorCode\":\"loyaltyNumber\",\"loyaltyNumber\":\"loyaltyNumber\"}]}}")
That's it

How can I save an object of a custom class in Userdefaults in swift 5/ Xcode 10.2

I want to save the array patientList in UserDefaults. Patient is an custom class so I need to transfer it into Data object, but this doesn't work on Swift 5 like it did before.
func addFirstPatient(){
let newPatient = Patient(name: nameField.text!, number: numberField.text!, resultArray: resultArray, diagnoseArray: diagnoseArray)
let patientList: [Patient] = [newPatient]
let encodeData: Data = NSKeyedArchiver.archivedData(withRootObject: patientList)
UserDefaults.standard.set(encodeData, forKey: "patientList")
UserDefaults.standard.synchronize()
}
struct Patient {
var diagnoseArray: [Diagnose]
var resultArray: [Diagnose]
var name: String
var number: String
init(name: String, number: String, resultArray: [Diagnose], diagnoseArray: [Diagnose]) {
self.diagnoseArray = diagnoseArray
self.name = name
self.number = number
self.resultArray = resultArray
}
}
struct Diagnose{
var name: String
var treatments: [Treatment]
var isPositiv = false
var isExtended = false
init(name: String, treatments: [Treatment]) {
self.name = name
self.treatments = treatments
}
}
struct Treatment {
var name: String
var wasMade = false
init(name: String) {
self.name = name
}
}
This is what the function looks like.
The problem is in the line where I initialize encodeData.
let encodeData: Data = try! NSKeyedArchiver.archivedData(withRootObject: patientList, requiringSecureCoding: false)
This is what Swift suggests but when I try it like this it always crashes and I don't get the error
You cannot use NSKeyedArchiver with structs at all. The objects must be subclasses of NSObject which adopt NSCoding and implement the required methods.
As suggested in the comments Codable is the better choice for example
struct Patient : Codable {
var name: String
var number: String
var resultArray: [Diagnose]
var diagnoseArray: [Diagnose]
}
struct Diagnose : Codable {
var name: String
var treatments: [Treatment]
var isPositiv : Bool
var isExtended : Bool
}
struct Treatment : Codable {
var name: String
var wasMade : Bool
}
let newPatient = Patient(name: "John Doe",
number: "123",
resultArray: [Diagnose(name: "Result", treatments: [Treatment(name: "Treat1", wasMade: false)], isPositiv: false, isExtended: false)],
diagnoseArray: [Diagnose(name: "Diagnose", treatments: [Treatment(name: "Treat2", wasMade: false)], isPositiv: false, isExtended: false)])
let patientList: [Patient] = [newPatient]
do {
let encodeData = try JSONEncoder().encode(patientList)
UserDefaults.standard.set(encodeData, forKey: "patientList")
// synchronize is not needed
} catch { print(error) }
If you want to provide default values for the Bool values you have to write an initializer.
Vadian's answer is correct, you cannot use NSKeyedArchiver with structs. Having all your objects conform to Codable is the best way to reproduce the behavior you are looking for. I do what Vadian does, but I you can also use protocol extensions to make this safer.
import UIKit
struct Patient: Codable {
var name: String
var number: String
var resultArray: [Diagnose]
var diagnoseArray: [Diagnose]
}
struct Diagnose: Codable {
var name: String
var treatments: [Treatment]
var isPositiv : Bool
var isExtended : Bool
}
struct Treatment: Codable {
var name: String
var wasMade : Bool
}
let newPatient = Patient(name: "John Doe",
number: "123",
resultArray: [Diagnose(name: "Result", treatments: [Treatment(name: "Treat1", wasMade: false)], isPositiv: false, isExtended: false)],
diagnoseArray: [Diagnose(name: "Diagnose", treatments: [Treatment(name: "Treat2", wasMade: false)], isPositiv: false, isExtended: false)])
let patientList: [Patient] = [newPatient]
Introduce a protocol to manage the encoding and saving of objects.
This does not have to inherit from Codable but it does for this example for simplicity.
/// Objects conforming to `CanSaveToDisk` have a save method and provide keys for saving individual objects or a list of objects.
protocol CanSaveToDisk: Codable {
/// Provide default logic for encoding this value.
static var defaultEncoder: JSONEncoder { get }
/// This key is used to save the individual object to disk. This works best by using a unique identifier.
var storageKeyForObject: String { get }
/// This key is used to save a list of these objects to disk. Any array of items conforming to `CanSaveToDisk` has the option to save as well.
static var storageKeyForListofObjects: String { get }
/// Persists the object to disk.
///
/// - Throws: useful to throw an error from an encoder or a custom error if you use stage different from user defaults like the keychain
func save() throws
}
Using protocol extensions we add an option to save an array of these objects.
extension Array where Element: CanSaveToDisk {
func dataValue() throws -> Data {
return try Element.defaultEncoder.encode(self)
}
func save() throws {
let storage = UserDefaults.standard
storage.set(try dataValue(), forKey: Element.storageKeyForListofObjects)
}
}
We extend our patient object so it can know what to do when saving.
I use "storage" so that this could be swapped with NSKeychain. If you are saving sensitive data (like patient information) you should be using the keychain instead of UserDefaults. Also, make sure you comply with security and privacy best practices for health data in whatever market you're offering your app. Laws can be a very different experience between countries. UserDefaults might not be safe enough storage.
There are lots of great keychain wrappers to make things easier. UserDefaults simply sets data using a key. The Keychain does the same. A wrapper like https://github.com/evgenyneu/keychain-swift will behave similar to how I use UserDefaults below. I have commented out what the equivalent use would look like for completeness.
extension Patient: CanSaveToDisk {
static var defaultEncoder: JSONEncoder {
let encoder = JSONEncoder()
// add additional customization here
// like dates or data handling
return encoder
}
var storageKeyForObject: String {
// "com.myapp.patient.123"
return "com.myapp.patient.\(number)"
}
static var storageKeyForListofObjects: String {
return "com.myapp.patientList"
}
func save() throws {
// you could also save to the keychain easily
//let keychain = KeychainSwift()
//keychain.set(dataObject, forKey: storageKeyForObject)
let data = try Patient.defaultEncoder.encode(self)
let storage = UserDefaults.standard
storage.setValue(data, forKey: storageKeyForObject)
}
}
Saving is simplified, check out the 2 examples below!
do {
// saving just one patient record
// this saves this patient to the storageKeyForObject
try patientList.first?.save()
// saving the entire list
try patientList.save()
} catch { print(error) }
struct Employee: Codable{
var name: String
}
var emp1 = Employee(name: "John")
let encoder = JSONEncoder()
do {
let data = try encoder.encode(emp1)
UserDefaults.standard.set(data, forKey: "employee")
UserDefaults.standard.synchronize()
} catch {
print("error")
}

UTF-8 encoding issue of JSONSerialization

I was trying convert struct to Dictionary in Swift. This was my code:
extension Encodable {
var dictionary: [String: Any]? {
if let data = try? JSONEncoder().encode(self) {
if let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
return dict
}
return nil
}
return nil
}
}
This works in most situation. But when I try to convert a nested structure which contains unicode characters such as Chinese, this happened:
struct PersonModel: Codable {
var job: String?
var contacts: [ContactSimpleModel]
var manager: ManagerSimpleModel?
}
struct ContactSimpleModel: Codable {
var relation: String
var name: String
}
struct ManagerSimpleModel: Codable {
var name: String
var age: Int
}
let contact1 = ContactSimpleModel(relation: "朋友", name: "宙斯")
let contact2 = ContactSimpleModel(relation: "同学", name: "奥丁")
let manager = ManagerSimpleModel(name: "拉斐尔", age: 31)
let job = "火枪手"
let person = PersonModel(job: job, contacts: [contact1, contact2], manager: manager)
if let dict = person.dictionary {
print(dict)
}
The result of this code is this:
["contacts": <__NSArrayI 0x600002471980>(
{
name = "\U5b99\U65af";
relation = "\U670b\U53cb";
},
{
name = "\U5965\U4e01";
relation = "\U540c\U5b66";
}
)
, "manager": {
age = 31;
name = "\U62c9\U6590\U5c14";
}, "job": 火枪手]
You can see the result. The Chinese characters in those nested structures were become a utf-8 encoding string. The top-level property "job": 火枪手 is right. But the values in those nested structures were not the original string.
Is this a bug of JSONSerialization? Or how to make it right?
More information. I used the result like this:
var sortedQuery = ""
if let dict = person.dictionary {
sortedQuery = dict.sorted(by: {$0.0 < $1.0})
.map({ "\($0)\($1)" })
.joined(separator: "")
}
It was used to check whether the query was legal. The result is not the same as Java or other platform.
The result is perfectly fine. That's the internal string representation – a pre-Unicode legacy – of an array or dictionary when you print it.
Assign the values to a label or text view and you will see the expected characters.

Can I use Swift's map() on Protocols?

I have some model code where I have some Thoughts that i want to read and write to plists. I have the following code:
protocol Note {
var body: String { get }
var author: String { get }
var favorite: Bool { get set }
var creationDate: Date { get }
var id: UUID { get }
var plistRepresentation: [String: Any] { get }
init(plist: [String: Any])
}
struct Thought: Note {
let body: String
let author: String
var favorite: Bool
let creationDate: Date
let id: UUID
}
extension Thought {
var plistRepresentation: [String: Any] {
return [
"body": body as Any,
"author": author as Any,
"favorite": favorite as Any,
"creationDate": creationDate as Any,
"id": id.uuidString as Any
]
}
init(plist: [String: Any]) {
body = plist["body"] as! String
author = plist["author"] as! String
favorite = plist["favorite"] as! Bool
creationDate = plist["creationDate"] as! Date
id = UUID(uuidString: plist["id"] as! String)!
}
}
for my data model, then down in my data write controller I have this method:
func fetchNotes() -> [Note] {
guard let notePlists = NSArray(contentsOf: notesFileURL) as? [[String: Any]] else {
return []
}
return notePlists.map(Note.init(plist:))
}
For some reason the line return notePlists.map(Note.init(plist:)) gives the error 'map' produces '[T]', not the expected contextual result type '[Note]'
However, If I replace the line with return notePlists.map(Thought.init(plist:)) I have no issues. Clearly I can't map the initializer of a protocol? Why not and what's an alternate solution?
If you expect to have multiple types conforming to Note and would like to know which type of note it is stored in your dictionary you need to add an enumeration to your protocol with all your note types.
enum NoteType {
case thought
}
add it to your protocol.
protocol Note {
var noteType: NoteType { get }
// ...
}
and add it to your Note objects:
struct Thought: Note {
let noteType: NoteType = .thought
// ...
}
This way you can read this property from your dictionary and map it accordingly.