I’ve an issue with a JSON
file the app crashes when it begins decoding and due to this fact the info is just not loaded. I downloaded it from Fb to check and I received the Fb JSON file with all the chums in my account and the format is that this:
{
"friends_v2": [
{
"name": "Test Test",
"timestamp": 1678361485
},
{
"name": "Test2 ",
"timestamp": 1678318229
},
{
"name": "Test 3",
"timestamp": 1675869376
}
]
}
As you possibly can see the file would not begin with identify
and timestamp
however earlier than them there’s “friends_v2“. I observed that if I delete that “friend_v2” the whole lot works in any other case it would not.
[
{
"name": "Test Test",
"timestamp": 1678361485
},
{
"name": "Test2 ",
"timestamp": 1678318229
},
{
"name": "Test 3",
"timestamp": 1675869376
}
]
In case I get uneditable JSON like this (so I am unable to delete “friend_v2“) how can I make the whole lot work? Thanks very a lot in your time
That is my JSONManager.swift
struct Associates: Codable {
var identify: String
var timestamp: Int
var contact_info: String?
static let allFriends: [Friends] = Bundle.predominant.decode(file: "mates.json")
static let allFriendsSample: Associates = allFriends[0]
}
extension Bundle {
func decode<T: Decodable>(file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Non è stato possibile trovare il file (file) nel progetto")
}
guard let knowledge = strive? Knowledge(contentsOf: url) else {
fatalError("Non è stato possibile caricare il file (file) nel progetto")
}
let decoder = JSONDecoder()
guard let loadData = strive? decoder.decode(T.self, from: knowledge) else {
fatalError("Non è stato possibile decodificare il file (file) nel progetto")
}
return loadData
}
}
And I take advantage of in ContentView
struct ContentView: View {
non-public var mates: [Friends] = Associates.allFriends
var physique: some View {
VStack {
Listing {
ForEach(mates, id: .identify) { good friend in
Textual content(good friend.identify)
}
}
}
.padding()
}
}