I am storing in a json file some user credentials (email, password, nickname). When I open the app on simulator and create multiple test accounts all works perfectly.
import Foundation
var users: [UserCredentials] = []
func save (email: String, password: String, nickname: String) {
let newUsers = UserCredentials(email: email, password: password, nickname: nickname)
users.append(newUsers)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
do {
let data = try encoder.encode(users)
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("users.json")
print(url.path)
try data.write(to: url)
} catch {
print("Error encoding JSON: \(error)")
}
}
The issue comes up when I reset the app and I type a new testuser. The json file resets instead of loading the testusers I saved before. Is there a way where the users stay saved in a file even when I restart the simulator?
Before reset:
```JSON
[
{
"email" : "beforereset@yahoo.com",
"nickname" : "beforereset12",
"password" : "test1234A"
},
{
"email" : "beforereset1@yahoo.com",
"nickname" : "beforereset123",
"password" : "test1234A"
}
]
```
After reset:
```JSON
[
{
"email" : "afterreset@yahoo.com",
"nickname" : "afterreset12",
"password" : "1234testA"
}
]
```
Where are the beforereset emails?